osx - Right way to define NSManagedObjectContext? -


earlier in apps coredata used define nsmanagedobjectcontext way:

class myviewcontroller: nsviewcontroller {      lazy var moc: nsmanagedobjectcontext? = {         let app = nsapplication.sharedapplication().delegate as! appdelegate         if let moc = app.managedobjectcontext {             return moc         } else {             return nil         }     }()      // .... } 

but downloaded apple's sample app called eartquakes , there nsmanagedobjectcontext that:

import cocoa  class coredatastackmanager {      class var sharedmanager: coredatastackmanager {         struct singleton {             static let coredatastackmanager = coredatastackmanager()         }         return singleton.coredatastackmanager     }      // persistenstorecoordinator etc... 

and in viewcontroller:

class applesviewcontroller: nsviewcontroller {      private lazy var managedobjectcontext: nsmanagedobjectcontext = {         let moc = nsmanagedobjectcontext(concurrencytype: .mainqueueconcurrencytype)          moc.persistentstorecoordinator = coredatastackmanager.sharedmanager.persistentstorecoordinator          return moc     }() } 

so wondering way right way - assume apple's way :) - , why core data stack defined in appdelegate default?

there many ways in end comes maintainability.

the best way have found , goes apple's traditional approach other items is:

  1. create separate controller core data.
  2. initialize controller in appdelegate.
  3. use dependency injection pass controller view controllers.

do not use singleton. while feels easy @ start of project tends paint corner later.


Comments

Popular posts from this blog

c++ - Difference between pre and post decrement in recursive function argument -

php - Nothing but 'run(); ' when browsing to my local project, how do I fix this? -

php - How can I echo out this array? -