Database Reference
In-Depth Information
NSError *error = nil;
if(![_managedObjectContext save:&error]){
NSLog(@"Error while trying to save data before store will change - %@",error.
localizedDescription);
}
}
[_managedObjectContext reset];
}
-(void)persistentStoresDidChange:(NSNotification *)notification {
[[NSNotificationCenter defaultCenter] postNotificationName:@"PERSISTENT_STORE_CHANGED"
object:nil];
}
-(void)persistentStoreDidImportContent:(NSNotification *)notification {
[_managedObjectContext mergeChangesFromContextDidSaveNotification:notification];
[[NSNotificationCenter defaultCenter] postNotificationName:@"PERSISTENT_STORE_UPDATED"
object:nil];
}
In the first method we are notified that the store will change. Because we still have access to
the store, we first check to see whether we have changes by calling [_managedObjectContext
hasChanges]; . If we do, then we try to save. If there is an error we use NSLog to log it for debugging.
Once saved, we call reset on our managedObjectContext to set it back to its base state.
In the second method, we post a notification to let our app know that we are using a new persistent
store. We will use this in just a little bit to update our UI.
In the final method we call the method mergeChangesFromContextDidSaveNotification : on our
managed object context and pass it the notification. This method handles the merging of changes
received from iCloud and will refresh, add, and/or remove any objects it needs to from the current
context. Finally, we post a notification letting our app know that the store has been updated in case
we need to do anything at that time.
Now let's move over to our CloseFriendsTableViewController.m file and add it as an observer for
those notifications. The viewDidLoad method should now look like this:
- (void)viewDidLoad
{
[super viewDidLoad];
[self loadCloseFriends];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(persistentStoreUpdated:)
name:@"PERSISTENT_STORE_UPDATED" object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(persistentStoreChanged:)
name:@"PERSISTENT_STORE_CHANGED" object:nil];
}
 
Search WWH ::




Custom Search