I am using a NSFetchedResultsController in combination with a UIManagedDocument which gets updated in an background thread.
I have set up the NSFetchedResultsController exactly as described in this tutorial: How To Use NSFetchedResultsController
I have set the delegate _fetchedResultsController.delegate = self and the protocol for my view controller to NSFetchedResultsControllerDelegate.
My code works fine when it loads the data after launch. However, the NSFetchedResultsController does not update the TableView whenever it has processed and saved the data in the background thread. In particular, the NSFetchedResultsController’s delegate methods -controllerWillChangeContent:controlleretc. are never being called.
I have double-checked that the SQLite database contains the data correctly.
This is how I process and save the data in the view controller:
[backgroundContext performBlock:^{
[company parseAttributesFrom:xmlStr inManagedObjectContext:backgroundContext]; //self.managedDocument.managedObjectContext
NSError *error = nil;
[backgroundContext save:&error];
if (error) NSLog(@"error: %@",error.localizedDescription);
[self.managedDocument.managedObjectContext performBlock:^{
NSError *error = nil;
[self.managedDocument.managedObjectContext save:&error];
if (error) NSLog(@"error: %@",error.localizedDescription);
}];
[self.managedDocument saveToURL:self.managedDocument.fileURL forSaveOperation:UIDocumentSaveForOverwriting completionHandler:NULL];
[self.managedDocument updateChangeCount:UIDocumentChangeDone];
}];
How can I get the NSFetchedResultsController to update the TableView automatically when the underlying data changes?
Thank you for your help!
I think that reason is in managedObjectContext. You make changes in background, and
NSFetchedResultsControllerfetches from the main one. So you need to merge changes to that one context by adding an observer of changes in contextHere is a great tutorial by Marcus Zarra – The Guru of Core Data)
Hope that helps.
http://www.cimgf.com/2011/08/22/importing-and-displaying-large-data-sets-in-core-data/