Eric Yu Eric Yu - 1 year ago 167
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 Source

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:

class AppDelegate: UIResponder, UIApplicationDelegate {

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

  func application(_ application: UIApplication,
    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) {

  func applicationWillTerminate(_ application: UIApplication) {

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

    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 {
      } catch let error as NSError {
        print("Error: \(error.localizedDescription)")

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)



Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download