Chapter 9. Why do you put keyword "lazy" before coreDataStack initialization?

As it mentioned by you guys:

      @UIApplicationMain
  class AppDelegate: UIResponder, UIApplicationDelegate {

   var window: UIWindow?
  lazy var coreDataStack = CoreDataStack(modelName: "SurfJournalModel")

  func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
_ = coreDataStack.mainContext

guard  let navigationController = window?.rootViewController as? UINavigationController,
  let listViewController = navigationController.topViewController as? JournalListViewController else {
    fatalError("Application Storyboard mis-configuration")
}

listViewController.coreDataStack = coreDataStack

return true
}

 func applicationWillTerminate(_ application: UIApplication) {
coreDataStack.saveContext()
} 
}

Isn’t keyword lazy excessive??? because CoredataStack already reference type it won’t copy as value type

@simons1994 Thanks very much for your question!

The “lazy” keyword allows you to create certain parts of a Swift type when needed, rather than doing it as part of its initialization process. This is used to improve performance when certain properties might be expensive to create.

I hope this helps!

All the best!

Hi,

Just in case if you want to understand why & when to use lazy keyword below link will clear your doubts for sure.
https://www.hackingwithswift.com/example-code/language/what-are-lazy-variables

Cheers!

This topic was automatically closed after 166 days. New replies are no longer allowed.