I wonder if you can help me to understand how to make this code "listen
" to changes from simperium
. When using NSFetchedResultsController
it all happens automatically, but how would I do with the following code? Tried to read the document but it was not really clear to me.
NSError *error;
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
fetchRequest.entity = [NSEntityDescription entityForName:kPersonEntity inManagedObjectContext:self.managedObjectContext];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"title == queen"];
NSArray *array = [[self.managedObjectContext executeFetchRequest:fetchRequest error:&error] filteredArrayUsingPredicate:predicate];
if(array.count > 0) {
_queen = (Person *)[array objectAtIndex:0];
}
Thanks!
You can listen to NSManagedObjectContextDidSaveNotification
notifications and act accordingly. Once Simperium processes and saves any changes, you'll receive a NSManagedObjectContextDidSaveNotification
notification. Listen for it:
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(contextSaved:) name:NSManagedObjectContextDidSaveNotification object:ctx];
Then, in your contextSaved:
method, you filter out the objects you care about
- (void)contextSaved:(NSNotification *)n
{
NSDictionary *userInfo = [n userInfo];
NSSet *objects = <see NSManagedObjectContext Change Notification User Info Keys>
<filter/process objects>
}
Also, don't forget to removeObserver
:
[[NSNotificationCenter defaultCenter] removeObserver:self];