Eric Yu Eric Yu - 3 months ago 36
Swift Question

accessing Core Data context in ViewController properly in iOS

I been doing

let context = (UIApplication.sharedApplication().delegate as! AppDelegate).managedObjectContext


and just use the context in my ViewControllers but in Apples iOS developer Library I read

"A view controller typically shouldn’t retrieve the context from a global object such as the application delegate"

so what is the right way to access the context from the AppDelegate, I am a little bit confused...

Answer

You are right, Apple doesn't recommend that way for iOS applications.

Creation of CoreData stack is the best solution:

So in your AppDelegate.swift:

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

  var window: UIWindow?
  lazy var coreDataStack = CoreDataStack()

  func application(_ application: UIApplication,
    didFinishLaunchingWithOptions
    launchOptions: [NSObject: Any]?) -> Bool {

    let navigationController =
self.window!.rootViewController as! UINavigationController

    let viewController =
navigationController.topViewController as! ViewController

    viewController.managedContext = coreDataStack.context

    return true
  }

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

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

Then, an example of CoreDataStack.swift (the class that holds all the duties about the MOC):

import CoreData    
class CoreDataStack {

  lazy var context: NSManagedObjectContext = {

    var managedObjectContext = NSManagedObjectContext(
      concurrencyType: .mainQueueConcurrencyType)

    managedObjectContext.persistentStoreCoordinator = self.psc
    return managedObjectContext
    }()

  fileprivate lazy var psc: NSPersistentStoreCoordinator = {

    let coordinator = NSPersistentStoreCoordinator(
      managedObjectModel: self.managedObjectModel)

    let url = self.applicationDocumentsDirectory
      .appendingPathComponent(self.modelName)

    do {
      let options =
      [NSMigratePersistentStoresAutomaticallyOption : true]

      try coordinator.addPersistentStore(
        ofType: NSSQLiteStoreType, configurationName: nil, at: url,
        options: options)
    } catch  {
      print("Error adding persistent store.")
    }

    return coordinator
    }()

  fileprivate lazy var managedObjectModel: NSManagedObjectModel = {

    let modelURL = Bundle.main
      .url(forResource: self.modelName,
        withExtension: "momd")!
    return NSManagedObjectModel(contentsOf: modelURL)!
    }()

  fileprivate lazy var applicationDocumentsDirectory: URL = {
    let urls = FileManager.default.urls(
      for: .documentDirectory, in: .userDomainMask)
    return urls[urls.count-1]
    }()

  func saveContext () {
    if context.hasChanges {
      do {
        try context.save()
      } catch let error as NSError {
        print("Error: \(error.localizedDescription)")
        abort()
      }
    }
  }
}

and finally an example of ViewController.swift:

import CoreData    
class ViewController: UIViewController {

  var managedContext: NSManagedObjectContext!      
  override func viewDidLoad() {        

    let myEntity = NSEntityDescription.entity(forEntityName: "MyEntityName",
      in: managedContext)

    ...

  }

}