Reputation: 423
I'm trying to set up coreData
but for some reason my context
returns nil
making it impossible to store
, fetch
or save
data. I have used the same context
code in another project an it works fine, so not sure what's up this time. The context looks like this:
func getContext() -> NSPersistentContainer {
let persistentContainer: NSPersistentContainer = {
let container = NSPersistentContainer(name: "MyContainer")
container.loadPersistentStores(completionHandler: { (storeDescription, error) in
if let error = error as NSError? {
fatalError("Unresolved error \(error), \(error.userInfo)")
}
})
return container
}()
return persistentContainer
}
Then in viewWillAppear
context = getContext().viewContext
print("🙊", context) //This is nil
So what am I doing wrong? Can the naming of the container
be whatever I want or does it has to be something specific?
Upvotes: 0
Views: 43
Reputation: 162
Name should be the same as .XCDataModel
file in your project.
In this case you should create container in that way:
let container = NSPersistentContainer(name: "CardGenerator")
Upvotes: 2