Search code examples
iosobjective-cxcodecore-datamogenerator

Exception when calling insertInManagedObjectContext:


I am creating a Reddit client for iOS for learning purposes. I am using CoreData, which I set up by following the CoreData Basics NSScreencast and I used Mogenerator to generate the model files.

I added a breakpoint to catch all exceptions and when I call the insertInManagedObjectContext: class method provided by Mogenerator, the app crashes on the following line:

return [NSEntityDescription insertNewObjectForEntityForName:@"AZRedditAccount" inManagedObjectContext:moc_];

This line is in the model that Mogenerator generated (_AZRedditAccount). When I look at the Variables View, I noticed an odd line which I don't really understand...

Odd line in the Variables View

What do I need to do to make things work please?

Edit: Here is what I have in my .xcdatamodeld file:

My .xcdatamodeld file

Edit 2: I added the whole project to Github, thought it might help solve the problem: https://github.com/AzizLight/Reddit


Solution

  • If you debug you see that your ManagedObjectContext (MOC) has no assigned PersistentStoreCoordinator (PSC) and therefore not model. In AZRedditDataModel.m you're initializing the PSC, but you're not retaining it (lines 84-109). If there's no model, the context cannot create any entities, because there's no "blueprint".