Application termination due to the uncaught exception "NSInvalidArgumentException", reason: "+ entityForName: nil is not legal NSManagedObjectContext

I get a null result for

Appdelegate.h

@property (readonly, strong, nonatomic) NSManagedObjectContext *managedObjectContext; @property (readonly, strong, nonatomic) NSManagedObjectModel *managedObjectModel; @property (readonly, strong, nonatomic) NSPersistentStoreCoordinator *persistentStoreCoordinator; 

AppDelegate.m

  @synthesize managedObjectContext = __managedObjectContext; @synthesize managedObjectModel = __managedObjectModel; @synthesize persistentStoreCoordinator = __persistentStoreCoordinator; - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { Person *newPerson = [NSEntityDescription insertNewObjectForEntityForName:@"Person" inManagedObjectContext:self.managedObjectContext]; ....... } 

I have one xcdatamodeld file with Person entity and name attribute. I created a Person.h and .m file from an NSManagedObject.

Why am I getting a null result for my output.

 Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '+entityForName: nil is not a legal NSManagedObjectContext parameter searching for entity name 'Person'' 
+6
source share
4 answers

I realized that since I did not add Core Data when I initially created the project, I was missing some kind of code that is automatically generated when a checkmark has a main data parameter. Thus, when I followed some online tutorials, this automatically generated code is assumed. What I messed up.

+2
source

In your viewController.m implementation file, right below this bit of code:

 - (void)viewDidLoad { 

add this bit of code:

 id delegate = [[UIApplication sharedApplication] delegate]; self.managedObjectContext = [delegate managedObjectContext]; 
+12
source

Make sure that the code that runs when an exception occurs runs in the same thread on which the managed entity context was created.

0
source

If you are using a project template that uses NSPersistentContainer, make sure you run it on iOS 10+, or it will fail to initialize and crash when performing any operation with master data.

0
source

Source: https://habr.com/ru/post/944322/


All Articles