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

I have added all the relevant code in the App Delegate, and I can add to the data model and extract from the data model in applicationDidFinishLaunchingWithOptions.

My problem arises when I try to write a data model in my view controller. I added this code to the header file:

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

And this code for 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 is going on? Any help would be appreciated.

+71
ios objective-c exception xcode core-data
Jul 21 '12 at 22:59
source share
8 answers

If you use segues, you will get the same problems if you don't pass the context line by line. Use this code in the prepareForSegue method of the class that triggers segue:

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

This assumes that you save your context in a property called "managedObjectContext".

+36
Dec 26 '12 at 16:09
source share

I forgot to pass the context to the view controller. Beginners mistake.

+46
Jul 21 '12 at 23:26
source share

You can pass the context by including the following code before you start retrieving data from the database:

 AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication]delegate]; context = [appDelegate managedObjectContext]; 
+45
Nov 24
source share

you should add this to your viewController:

  id delegate = [[UIApplication sharedApplication] delegate]; self.managedObjectContext = [delegate managedObjectContext]; 
+20
Jan 20 '13 at 7:53
source share

I have this problem and a colleague helped me. If you get this error message: "entityForName: nil is not a legitimate NSManagedObjectContext parameter, looking for the name of the entity." And you made changes to the coredata model. I think the problem may not be code .

The solution may be simple. Try one of the following options:

  • Just uninstall the application from the device you are testing, it should have an old version of your model.
  • Create a different version of the database using Xcode,> Editor> Add Model Version.

Hope this helps.

+2
May 27 '14 at 17:31
source share

If the destination view controller is built into the NavigationController, the context should be set accordingly as follows:

  self.mydetailViewController = [[[segue destinationViewController] viewControllers] objectAtIndex:0]; [self.mydetailViewController setManagedObjectContext:self.managedObjectContext]; 
0
Mar 14 '14 at 17:08
source share

In my case .xcdatamodeld was incorrectly marked in AppDelegate:

  let container = NSPersistentContainer(name: "name of data model") 
0
Jan 15 '17 at 17:06
source share

I am a fan of lazy initialization. Thus, if you need to introduce a new context for testing, you can get it from the application delegate if you configured 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; } 
0
Jan 20 '17 at 15:09 on
source share



All Articles