How to include NSPersistentContainer in AppDelegate

13,344

You should firstly import CoreData framework and then write this code in AppDelegate.swift:

lazy var persistentContainer: NSPersistentContainer = {

    let container = NSPersistentContainer(name: "Your Model File Name")
    container.loadPersistentStores(completionHandler: { (storeDescription, error) in
        if let error = error {

            fatalError("Unresolved error, \((error as NSError).userInfo)")
        }
    })
    return container
}()

And then you should write this:

 let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
Share:
13,344
Karthikeyan Sk
Author by

Karthikeyan Sk

Updated on July 28, 2022

Comments

  • Karthikeyan Sk
    Karthikeyan Sk over 1 year

    I'm receiving an error:

    AppDelegate has no member persistentContainer

    import UIKit
    import CoreData
    
    class ViewController: UIViewController {
    
        override func viewDidLoad() {
            super.viewDidLoad()
            let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
            let context = appDelegate.persistentContainer.viewContext // Error: value of type 'AppDelegate' has no member 'persistentContainer'
        }
    
    }
    

    In AppDelegate.swift file, NSPersistentStoreCoordinator is defined as default.

    lazy var persistentStoreCoordinator: NSPersistentStoreCoordinator = {
        let coordinator = NSPersistentStoreCoordinator(managedObjectModel: self.managedObjectModel)
        let url = self.applicationDocumentsDirectory.URLByAppendingPathComponent("SingleViewCoreData.sqlite")
        var failureReason = "There was an error creating or loading the application's saved data."
        do {
            try coordinator.addPersistentStoreWithType(NSSQLiteStoreType, configuration: nil, URL: url, options: nil)
        }
        catch {
            var dict = [String: AnyObject]()
            dict[NSLocalizedDescriptionKey] = "Failed to initialize the application's saved data"
            dict[NSLocalizedFailureReasonErrorKey] = failureReason
            dict[NSUnderlyingErrorKey] = error as NSError
            let wrappedError = NSError(domain: "YOUR_ERROR_DOMAIN", code: 9999, userInfo: dict)
            NSLog("Unresolved error \(wrappedError), \(wrappedError.userInfo)")
            abort()
        }
        return coordinator
    }()
    
  • Karthikeyan Sk
    Karthikeyan Sk over 7 years
    i have already imported CoreData framework. now tried writing your code too . it says "use of undeclared type 'NSPersitentContainer'. Even in AppDelegate.swift file, the default class method 'NSPersistentStoreCoordinator' is shown instead 'NSPersistenContainer'.
  • Pragnesh Vitthani
    Pragnesh Vitthani over 7 years
    Pls, write above persistentContainer class method in your AppDelegate...
  • Karthikeyan Sk
    Karthikeyan Sk over 7 years
    i did. it reports ' use of undeclared type NSPersistentContainer '
  • Pragnesh Vitthani
    Pragnesh Vitthani over 7 years
    but in your question you declare persistentStoreCoordinator and in viewDidLoad() you use persistentContainer.
  • Karthikeyan Sk
    Karthikeyan Sk over 7 years
    Thanks i got it @pragnesh Vitthani