4

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

fabian789
  • 8,348
  • 4
  • 45
  • 91
  • I'm having the same lock up ever side iOS 5 came out. Once I change an entry in my detail view, I go back to the main table and it's locked up. My app worked fine on iOS 4. It now locks up every time the user edits a row in the table. Something new with iOS 5? What changed? – RyeMAC3 Jan 13 '12 at 01:22

1 Answers1

4

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.

John Topley
  • 113,588
  • 46
  • 195
  • 237
deanWombourne
  • 38,189
  • 13
  • 98
  • 110
  • Do I have to somehow sync the different `ManagedObjectContext` s when I am done importing or will the `PersistentStoreCoordinator` do this? – fabian789 Dec 12 '10 at 17:28
  • 2
    When you call save on the managed object context it will update the underlying database and generate a NSManagedObjectContextObjectsDidChangeNotification notification. however, I've never used that, I've always done the notifiying myself (either through a delegate or a notification of my own). You will have to watch out for passing objects between threads because that causes random crashes - pass their ids back to the main thread and get them from the main threads managed object context. – deanWombourne Dec 12 '10 at 17:31
  • 1
    OK, I am going to code now. You might want to add this to your answer, it looks rather helpful: http://www.duckrowing.com/2010/03/11/using-core-data-on-multiple-threads/ – fabian789 Dec 12 '10 at 17:39
  • Forget adding it to my answer - I'm adding it to my bookmarks! – deanWombourne Dec 12 '10 at 17:47
  • Excuse me but did this fix your problem ? I am getting the exact same error, yet I am not multithreading. – Daniel Jul 31 '12 at 02:12