Last Updated: February 25, 2016
·
1.968K
· nebiros

+entityForName: could not locate an NSManagedObjectModel for entity name

I just implement CoreData for an existing app that I'm working on it, a tabbed one, so, in my ViewController I need to save something using a NSManagedObject, at first time, I got this error:

+entityForName: could not locate an NSManagedObjectModel for entity name 'CMFavorite'

Seems like a common error, reading about it I found this post, then I check if I have some entities inside the managed object model, and BOOM!, NIL!, so, what to do?, what to do?, the managed object context was nil too!, arggghhhhh!, thankfully I can get and instance of managed object context from my AppDelegate inside this ViewController, ;-), so, here's the code:

- (NSManagedObjectContext *)managedObjectContext
{
    if ( _managedObjectContext == nil) {
        _managedObjectContext = [[[UIApplication sharedApplication] delegate] performSelector:@selector(managedObjectContext)];
    }

    return _managedObjectContext;
}