+entityForName: nil is not a legal NSManagedObjectContext parameter searching for entity name ‘Account”

- (NSManagedObjectContext *)managedObjectContext
{
    if (managedObjectContext != nil) return managedObjectContext;

    NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator];
    if (coordinator != nil) {

        managedObjectContext = [[NSManagedObjectContext alloc] init];
        [managedObjectContext setPersistentStoreCoordinator:coordinator];
    }
    return managedObjectContext;
}
  • You haven’t provided a lazy loading implementation of persistentStoreCoordinator
  • so coordinator will always be nil
  • so you will always be returning nil from this method
  • which means you will always get the error above.

To explain the error:

+entityForName: nil is not a legal NSManagedObjectContext parameter searching for entity name ‘Account’

It’s not immediately obvious from reading it, but this means that nil is not a legal thing to pass for the managed object context. On first reading, it looks like you’re doing entityForName:nil but that isn’t the case.

To fix the problem, you will need to provide a valid persistent store coordinator. I have a small article here which explains just how little code you need to set up a core data stack, this may help you.

Leave a Comment