views:

355

answers:

1

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.

+4  A: 

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.

Marcus S. Zarra