views:

1993

answers:

4

I'm working on an application that holds its data in an external MySQL server, but caches it locally using Core Data for better response times. Basically, what I'd like to do is this:

  1. Fetch data from Core Data (SQLite data store, using NSFetchedResultsController) and display it
  2. Grab new items from the MySQL server in the background
  3. Refresh the current table view with the new set of data (both new and old items)

I have all this working except the last step. I can't quite figure out how to make an NSFetchedResultsController update its set of data. So far, I've tried adding items directly to its NSManagedObjectContext:

[NSEntityDescription insertNewObjectForEntityForName:@"Entity"
                              inManagedObjectContext:[fetchedResultsController
                                                      managedObjectContext]];

I've also tried what Apple does in their CoreDataBooks example, and used a separate "adding" managed object context and a call to mergeChangesFromContextDidSaveNotification:. Neither seems to change the set of NSManagedObject*s currently in my fetched result controller's managed object context.

How would I go about updating the set of objects an NSFetchedResultsController currently manages?

+2  A: 

I found out my issue was not that the objects weren't updating, but that the NSFetchedResultsController instance I had wasn't updating its section index titles properly, and I therefore couldn't see the results in my UITableView.

Updating a managed object context from a fetched results controller does update the controller's result object set.

Tim
+1  A: 

just have your problem and yes apparently using notifications is the unique way you can make a tableview refresh when the table used a nsfetchedresults controller.

like in the core databooks sample:
step1: add an observer to the NSNotificationCenter for the notification NSManagedObjectContextDidSaveNotification
step2: save your context (the notification trigger to your selector)
step3: in your selector method: merge the changes in the context using the method mergeChangesFromContextDidSaveNotification
step4: remove the observer from the notification center.

Personally I would like to bypass the notification certer and only tell the context refresh yourself dammit :)

This is indeed the method I used, and I think this is the way Apple does it in some of their Core Data examples.
Tim
A: 

You mention you were not updating your tableview sections indexes properly. I have the similar issue. My fetechesResultsController updates the data displayed in the table view and the sections but not the section indexes. Any idea why? What did you do to update your section indexes?

Thanks in adavance

Vincent Gaulet
There's a known bug in the 3.0 SDK where a Core Data-backed table view won't update its table section indexes if you're using an NSFetchedResultsController in your table's data source. You can upgrade to the 3.1 SDK, or work around the issue by iterating over the fetched results controller's sections on your own, building an array of titles yourself.
Tim
Many thanks Tim. I lost 4 days of work on this. I don't know how I managed to miss this bug in the documentation.
Vincent Gaulet
Indeed. I have updated Iphone SDK with latest version and my section index is now fine under 3.1 (still bugs on 3.0).I can't thank you enough, Tim. :-)
Vincent Gaulet
A: 

I have 3.1 SDK installed but my section indexes are still not getting updated. I have xCode 3.1.4 and SDK 3.1, and trying to figure out if there is a later version of SDK I am missing. But it looks like I am up to date. I am having the exact same problem as above, that the fetchedResultsController updates that data and the sections but not the section indexes. This is so confusing. Can anyone shed light?

jp
Are you building for 3.0 or 3.1? As per Vincent Gaulet's answer and comments below, you shouldn't be seeing this bug anymore. Double-check that you're building with the proper SDK, optionally reinstall Xcode and the SDK, then if you're still having an issue file a bug with Apple.
Tim