开发者

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开发者_如何学JAVA 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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜