+entityForName: nil is not a legal NSManagedObjectContext parameter searching for entity name 'Account''

后端 未结 5 1596
日久生厌
日久生厌 2020-12-01 20:52

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

相关标签:
5条回答
  • 2020-12-01 21:15

    In my case, I'm using multiple contexts (parent/child) with different concurrency types to improve performance. I have three contexts:

    1. storeContext which is the only context whose persistentStoreCoordinator had been set.
    2. viewContext whose parent is storeContext
    3. 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.

    0 讨论(0)
  • 2020-12-01 21:19

    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. :)

    0 讨论(0)
  • 2020-12-01 21:22
    - (NSManagedObjectContext *)managedObjectContext
    {
        if (managedObjectContext != nil) return managedObjectContext;
    
        NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator];
        if (coordinator != nil) {
    
            managedObjectContext = [[NSManagedObjectContext alloc] init];
            [managedObjectContext setPersistentStoreCoordinator:coordinator];
        }
        return managedObjectContext;
    }
    
    • You haven't provided a lazy loading implementation of persistentStoreCoordinator
    • so coordinator will always be nil
    • so you will always be returning nil from this method
    • which means you will always get the error above.

    To 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.

    0 讨论(0)
  • 2020-12-01 21:32

    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.

    0 讨论(0)
  • 2020-12-01 21:34

    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.

    0 讨论(0)
提交回复
热议问题