NSManagedObjectContextObjectsDidChangeNotification userInfo Dictionary
I am using the NSManagedObjectContextObjectsDidChangeNotification notfication in my app, I already now how to use it. As I have used the below code to add the observer …
- (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:@selecto开发者_运维知识库r(syncKVO:)
name:NSManagedObjectContextObjectsDidChangeNotification
object:nil];
}
But I would like to check the userInfo dictionary to make sure the method actually has to be triggered, How would I do this?
Looking at the documentation for NSManagedObject
gives you an answer.
A notification has three instance methods one of which is the -userInfo
method which returns the userInfo dictionary
.
It looks like your syncKVO: method is incorrect; notification handlers should take the notification object as a parameter.
The documentation for the notification you are looking for shows the keys that are in this dictionary for this notification and you can use something like this to get what you might need:
- (void)syncKVO:(NSNotification *)notification {
NSDictionary *userInfoDictionary = [notification userInfo];
NSSet *deletedObjects = [userInfoDictionary objectForKey:NSDeletedObjectsKey];
// do what you want with the deleted objects
}
精彩评论