I’m working on a sync process where the latest changed objects are loaded from a server with RestKit using a timestamp and saving all changes using Core Data based on the MOC idea.
Sequence
1) Starting the sync process and save changes to Core Data [AppDelegate]
2) Refetching data from Core Data after process is finished and reload data [ContactTableView]
3) Reloading input views when fetch is complete [ContactDetailView]
4) Reloading table view placed in ViewContainer inside of ContactDetailView [ContactDetailTV]
Passing data between views
ContactTableView (passing object when user selects row)
--» ContactDetailView (passing object to subView in viewContainer)
--» ContactDetailTV (displaying data of object in a tableView)
Problem
That works
When I’m inside of ContactTableView the table view is reloaded with the new fetched data and everything is fine.
That doesn’t work
When I’m inside of ContactDetailTV (because the data can change when I’m looking at it) the view displays the old data inside of the ContactDetailTV even after calling [self.tableView reloadData].
Question
What am I doing wrong or what is missing to get the reloading to work like expected?
Code
AppDelegate
- (void)startSyncProcess:(NSInteger)operationCode
{
// Init helper
syncCount = 0;
syncProcess = 0;
switch (operationCode)
{
default:
// (1) Request deleted companys from server
[self requestDeletedCompanysFromServer];
break;
case 0:
// (2) Request deleted contacts from server
[self requestDeletedContactsFromServer];
break;
case 1:
// (2) Request edited companys from server
[self requestEditedCompanysFromServer];
break;
case 2:
// (2) Request edited contacts from server
[self requestEditedContactsFromServer];
break;
case 3:
// Reload data views
[self saveCurrentSyncTime];
[[NSNotificationCenter defaultCenter] postNotificationName:@"updateTableViewAfterSave" object:self];
break;
}
}
ContactTableView
- (void)syncContextDidSave:(NSNotification*)saveNotification
{
// Handling the "updateTableViewAfterSave"-notification by refetching the data with Core Data
NSError *error;
if (![[self fetchedResultsController] performFetch:&error])
NSLog(@"[ERROR] Saving failed with error: %@", [error userInfo]);
[[NSNotificationCenter defaultCenter] postNotificationName:@"updateSubViewAfterSave" object:nil];
}
ContactDetailTV
- (void)updateSubViewAfterSave:(NSNotification *)saveNotification
{
// Handling the "updateSubViewAfterSave"-notification by updating the table view that displays the data without using a fetchedResultsController
[self.tableView reloadData];
}
To update the object with the newest data from Core Data (because even when refetching the data, the context of the fetchedController keeps a “snapshot” of the object), I had to update the method.