我想知道您是否可以帮助我了解如何使此代码“listen”变为simperium。使用NSFetchedResultsController时,所有操作都会自动发生,但是如何处理以下代码?试图阅读该文档,但对我来说还不是很清楚。

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];
}

谢谢!

最佳答案

您可以听NSManagedObjectContextDidSaveNotification通知并采取相应措施。 Simperium处理并保存所有更改后,您将收到NSManagedObjectContextDidSaveNotification通知。听它:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(contextSaved:) name:NSManagedObjectContextDidSaveNotification object:ctx];

然后,在contextSaved:方法中,过滤掉您关心的对象
    - (void)contextSaved:(NSNotification *)n
    {
        NSDictionary *userInfo = [n userInfo];
        NSSet *objects = <see NSManagedObjectContext Change Notification User Info Keys>
        <filter/process objects>
    }

另外,不要忘记removeObserver:
[[NSNotificationCenter defaultCenter] removeObserver:self];

关于ios - 如何使用simperium“监听”对特定对象的更改,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/16436155/

10-09 09:38