'+entityForName: nil is not a legal NSManagedObjectContext parameter - Core Data

Objective CIosXcodeExceptionCore Data

Objective C Problem Overview


I have added all of the relevant code to the App Delegate, and I am able to add to the data model and fetch from the data model in applicationDidFinishLaunchingWithOptions.

My problem comes when I am trying to write to the data model in my View Controller. I have added this code to the header file:

NSFetchedResultsController *fetchedResultsController;
NSManagedObjectContext *managedObjectContext;

@property (nonatomic, retain) NSFetchedResultsController *fetchedResultsController;
@property (nonatomic, retain) NSManagedObjectContext *managedObjectContext;

And this code to my implementation file:

NSManagedObjectContext *context = [self managedObjectContext];
NSManagedObject *model = [NSEntityDescription
                          insertNewObjectForEntityForName:@"Events" 
                          inManagedObjectContext:context];
[model setValue:@"Sample Event" forKey:@"eventName"];

NSError *error;
if (![context save:&error]) {
    NSLog(@"Couldn't save: %@", [error localizedDescription]);
}

However, I get the following error:

'NSInvalidArgumentException', reason: '+entityForName: nil is not a legal NSManagedObjectContext parameter searching for entity name 'Events''

Does anyone know what's going on? Any help would be appreciated.

Objective C Solutions


Solution 1 - Objective C

I had forgotten to pass the context to the view controller. Rookie error.

Solution 2 - Objective C

You can pass the context by including the following code before you begin to fetch the data form the database:

AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication]delegate];
context = [appDelegate managedObjectContext];

Solution 3 - Objective C

If you are using segues you will get the same problems if you don't pass the context down the line. Use this code in the prepareForSegue method of class initiating the segue:

[[segue destinationViewController] setManagedObjectContext:self.managedObjectContext];

That assumes you hold your context in a property called "managedObjectContext" of course.

Solution 4 - Objective C

you should add this to your viewController:

 id delegate = [[UIApplication sharedApplication] delegate];
    self.managedObjectContext = [delegate managedObjectContext];

Solution 5 - Objective C

I got this problem and a colleague helped me out. If you got this error message: "entityForName: nil is not a legal NSManagedObjectContext parameter searching for entity name". And you made changes in you coredata model. I think the problem might not be the code.

The solution can be simple. Try one of those options:

  • Just delete the app from the device you are testing, it should have the old version of your model.
  • Create another database version using Xcode, >Editor>Add Model Version.

Hope it helps.

Solution 6 - Objective C

In my case the .xcdatamodeld was mislabeled in the AppDelegate:

 let container = NSPersistentContainer(name: "name of data model")

Solution 7 - Objective C

If the destination view controller is embedded in a NavigationController, the context needs to be set appropriately as follows-

  self.mydetailViewController = [[[segue destinationViewController] viewControllers] objectAtIndex:0];
 [self.mydetailViewController setManagedObjectContext:self.managedObjectContext];

Solution 8 - Objective C

I'm a fan of lazy initialization. This way if you need to inject a new context for testing you can, or it'll get it's context from the app delegate if you set up your MOC there.

class.h
@property (strong, nonatomic,getter=getManagedObjectContext) NSManagedObjectContext *managedObjectContext;

class.m
    -(NSManagedObjectContext *)getManagedObjectContext {
        if (_managedObjectContext) {
            return _managedObjectContext;
        }
        _managedObjectContext = [[(AppDelegate *)[[UIApplication sharedApplication]delegate]sharedDataModel]managedObjectContext];
        return _managedObjectContext;
    }

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAlex GodbehereView Question on Stackoverflow
Solution 1 - Objective CAlex GodbehereView Answer on Stackoverflow
Solution 2 - Objective CVishwaniView Answer on Stackoverflow
Solution 3 - Objective CTimView Answer on Stackoverflow
Solution 4 - Objective CHashem AboonajmiView Answer on Stackoverflow
Solution 5 - Objective CrafaeljuzoView Answer on Stackoverflow
Solution 6 - Objective CcamelCaseUpInYourFaceView Answer on Stackoverflow
Solution 7 - Objective ChackerinheelsView Answer on Stackoverflow
Solution 8 - Objective CSpaceTruckerView Answer on Stackoverflow