I have tried a lot of options, but can\'t find the solution for this problem. I created a Core Data file and named the entity Account, Created an string attribute called use
In my case, I'm using multiple contexts (parent/child) with different concurrency types to improve performance. I have three contexts:
storeContext
which is the only context whose persistentStoreCoordinator
had been set.viewContext
whose parent is storeContext
backgroundContext
whose parent should have been viewContext
but I forgot to set backgroundContext.parent = viewContext
. Saving an entity on the backgroundContext
produced the same error...
+entityForName: nil is not a legal NSManagedObjectContext parameter searching for entity name...
...because backgroundContext
wasn't a part of the parent/child context chain.
Setting backgroundContext
's parent
to viewContext
established the chain back to the persistent store coordinator and resolved the error.
I encountered this entityForName: nil
error, but it ended up being somewhat of a red herring that only manifested itself when running unit tests on my CI. During testing, the app was encountering some weird threading conditions caused by NSAttributedString's HTML Importer. An asynchronous dispatch onto the main queue to interact with Core Data was happening just as the NSAttributedString
was being created from HTML.
Just posting my experience here in case it ends up helping someone else. :)
- (NSManagedObjectContext *)managedObjectContext
{
if (managedObjectContext != nil) return managedObjectContext;
NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator];
if (coordinator != nil) {
managedObjectContext = [[NSManagedObjectContext alloc] init];
[managedObjectContext setPersistentStoreCoordinator:coordinator];
}
return managedObjectContext;
}
persistentStoreCoordinator
coordinator
will always be nil
nil
from this methodTo 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.
I encountered the same error while fetching data from Core Data. The reason was value to context object was not set properly and It was setting to it after fetching the result. So, setting context properly before hitting fetch request makes the work done.
It might happens because of mismatch of entity name and its class name. Ensure that your Account entity has an appropriate class name in .xcdatamodeld file.