I use NSFetchedResultsControllerin conjunction with UIManagedDocumentwhich is updated in the background thread.
I set up NSFetchedResultsController exactly as described in this tutorial: How to use NSFetchedResultsController
I set the delegate _fetchedResultsController.delegate = selfand protocol for my view controller NSFetchedResultsControllerDelegate.
My code works fine when it loads data after launch. However, it NSFetchedResultsController does not update the TableView whenever it has processed and stored data in the background thread. In particular, the delegation methods of NSFetchedResultsController -controllerWillChangeContent:controller, etc. never called.
I double checked that the SQLite database contains the data correctly. This is how I process and save data in the view controller:
[backgroundContext performBlock:^{
[company parseAttributesFrom:xmlStr inManagedObjectContext:backgroundContext];
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 NSFetchedResultsController to update the TableView automatically when the underlying data changes?
Thank you for your help!
Alexr source
share