NSManagedObjectContextObjectsDidChangeNotification userInfo Dictionary

Jos*_*hua 1 cocoa notifications objective-c

我在我的应用程序中使用NSManagedObjectContextObjectsDidChangeNotification notfication,我现在已经如何使用它.因为我使用下面的代码添加观察者...

- (void) awakeFromNib {
NSNotificationCenter *nc;
nc = [NSNotificationCenter defaultCenter];

[nc addObserver:self
       selector:@selector(syncKVO:)
           name:NSManagedObjectContextObjectsDidChangeNotification
         object:nil];
}

- (void)syncKVO:(id)sender {
NSNotificationCenter *nc;
nc = [NSNotificationCenter defaultCenter];
[nc removeObserver:self
              name:NSManagedObjectContextObjectsDidChangeNotification
            object:nil];

// Do stuff.

[nc addObserver:self
       selector:@selector(syncKVO:)
           name:NSManagedObjectContextObjectsDidChangeNotification
         object:nil];

}
Run Code Online (Sandbox Code Playgroud)

但我想检查userInfo字典以确保实际必须触发该方法,我该怎么做?

Abi*_*ern 6

查看文档可以NSManagedObject给出答案.

通知有三个实例方法,其中一个是-userInfo返回的方法userInfo dictionary.

看起来你的syncKVO:方法不正确; 通知处理程序应将通知对象作为参数.

您要查找的通知的文档显示了此通知的字典中的键,您可以使用类似的内容来获取您可能需要的内容:

- (void)syncKVO:(NSNotification *)notification {
    NSDictionary *userInfoDictionary = [notification userInfo];
    NSSet *deletedObjects = [userInfoDictionary objectForKey:NSDeletedObjectsKey];

    // do what you want with the deleted objects
}
Run Code Online (Sandbox Code Playgroud)