Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSFetchedResultsController not showing updates from other contexts

I have an NSFetchedResultsController set up to populate a UITableView, based off my 'main' NSManagedObjectContext.

In a timer, I continually add objects into a separate NSManagedObjectContext, but one that shares its NSPersistantStoreCoordinator with the main context.

I can see NSManagedObjectContextWillSaveNotification notifications being sent, and when I quit an restart the app, I have all the data from the previous run. However, no updates happen to the current NSFetchedResultsController.

I've created a test project and placed it here.



(I initially thought this was happening due to there being multiple threads at play; however, it happens when everything is done in one thread).

Original Question

I have an NSFetchedResultsController set up to populate a UITableView, based off my 'main' NSManagedObjetContext.

In a background thread, I download and import several objects into a separate NSManagedObjectContext, but one that shares its NSPersistantStoreCoordinator with the main thread's context.

I am registered to receive NSManagedObjectContextWillSaveNotification notifications. When I get these, I forward them on to the main thread, and pass them to my main context.

Using the debugger, I can watch these come in, and verify that they are, indeed, adding objects to the context. However, my NSFetchedResultsController is not updating. If I do this all in the main thread, it does work. Calling -performFetch: on the controller will cause it to update, so I know it's not that it's not seeing the new objects.

Most of this is boilerplate code, just split into different threads.

like image 358
Ben Gottlieb Avatar asked Jan 28 '10 14:01

Ben Gottlieb


1 Answers

Are you getting any delegate call backs from the NSFetchedResultsController? If not then this may be a bug with the NSFetchedResultsController itself. I would recommend creating a test project that can confirm/deny this issue in isolation.

If you can duplicate it in isolation;

  1. I would love to see it :)
  2. File a radar with Apple.

After reviewing the code, the answer is to change your notification observer from NSManagedObjectContextWillSaveNotification to NSManagedObjectContextDidSaveNotification. You were sending the message just before the save occurred which was causing your issues.

like image 81
Marcus S. Zarra Avatar answered Sep 18 '22 18:09

Marcus S. Zarra