简体   繁体   中英

NSFetchedResultsController and NSOperation

In a UITableViewController , I use an NSFetchedResultsController for my data. Everything works fine, except for when I start importing some objects in a separate thread: I use an NSOperationQueue in which I insert objects into my ManagedObjectContext . This happens in a separate view. The NSFetchedResultsController doesn't seem to like this and writes to the console:

Serious application error. An exception was caught from the delegate of NSFetchedResultsController during a call to -controllerDidChangeContent:. Attempt to create two animations for cell with userInfo (null)

Apparently it tries to fetch the new objects.

On the topic of concurrency, the Core Data Programming Guide says something like using a ManagedObjectContext for each thread, but that sounds rather complicated.

I now don't know whether I should actually create my own NSOperation subclass, creating a ManagedObjectContext in it and so on, or whether it is possible to prevent the NSFetchedResultsController from updating for some time?

I would appreciate some help, Fabian

You need a NSManagedObjectContext per thread, sorry!

It's not just the NSFetchesResultsController that will be accessing your context - coreData won't fetch some data until it's needed to your context might be accessed at any point.

However, it's only the context that you need to create on a per thread basis. Just write a method on your delegate that creates a managed object context and call that in each of your NSOperations - this will make them per thread instead of all using the same one.

The managed context on your main thread can be created with this method as well.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM