FetchedResultsController Swift 3 API Misuse: Attempt to serialize store access on non-owning coordinator

梦想与她 提交于 2019-12-03 10:54:22

I have reviewed your all comments and contents posted here. You have not shared one file here, but the problem is occurring you are creating invalid managed objects in the context.

And then whenever you call viewWillAppear() function in InventoryViewController, it saves the context.

Finally, it synced empty records into your database. During parsing those invalid objects, it tried to parse nil value, so crashed.

Please never set default value for managed objects you are defining as properties. I hope this will clarify your issue.

I was running into similar issue and i moved to the new CoreData api introduced in ios10. This uses the NSPersistentContainer class to create the stack and create associated contexts. This eliminates the need to manually call save or order the creation of fetch results controller.

Good blog post to read: https://useyourloaf.com/blog/easier-core-data-setup-with-persistent-containers/

My setup is a follows

create a store NSPersistentContainer

let persistentContainer = NSPersistentContainer(name: "ModelFileName");

configure settings

let url = NSPersistentContainer.defaultDirectoryURL()
let path = url.appendingPathComponent(persistentContainer.name);
description.shouldAddStoreAsynchronously = true; //write to disk should happen on background thread
self.persistentContainer.persistentStoreDescriptions = [description];

load the store

persistentContainer.loadPersistentStores(completionHandler: { (storeDescription, error) in
        if let error = error {
              fatalError("Unresolved error \(error), \(error.localizedDescription)")
        }

    //configure context for main view to automatically merge changes
    persistentContainer.viewContext.automaticallyMergesChangesFromParent = true;
});

in the view controller you can access the view context by calling

persistentContainer.viewContext

if you need to make changes you can call

persistentContainer.performBackgroundTask({ (context) in ... });

or you can get a background context

let context = persistentContainer.newBackgroundContext()
context.perform({ ... })

In case this helps anyone else who gets the "API Misuse: Attempt to serialize store access on non-owning coordinator" error - I was getting the error because I accessed an object in a singleton that had not been destroyed and was still using the old NSManagedObjectContext after I reset the NSPersistentStore and NSManagedObjectContext.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!