Update results of NSFetchedResultsController without a new fetch
Asked Answered
H

2

7

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?

Haversack answered 15/6, 2009 at 2:59 Comment(0)
H
4

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.

Haversack answered 16/6, 2009 at 2:0 Comment(0)
S
7

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 :)

Sixtynine answered 1/7, 2009 at 14:2 Comment(1)
This is indeed the method I used, and I think this is the way Apple does it in some of their Core Data examples.Haversack
H
4

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.

Haversack answered 16/6, 2009 at 2:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.