我正在尝试iPhone SDK 3.1.3上的多线程核心数据实现。我会为每个线程两种不同的NSManagedObjectContext对象,我喜欢下面登记的变更通知在一个线程中:缺少核心数据通知
- (void)setup {
DLog(@"Registering for NSManagedObjectContext notifications");
NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
[nc addObserver:self selector:@selector(test:) name:@"NSManagedObjectContextWillSaveNotification" object:nil];
[nc addObserver:self selector:@selector(test:) name:@"NSManagedObjectContextDidSaveNotification" object:nil];
[nc addObserver:self selector:@selector(test:) name:@"NSManagedObjectContextObjectsDidChangeNotification" object:nil];
}
- (void)test:(NSNotification *)notif {
DLog(@"Test callback");
}
在我的其他线程我保存了第二个方面,像这样:
NSError *error = nil;
[managedObjectContext save:&error];
if (error) {
ALog(@"Error occured while trying to save a NewsStory object");
}
else {
DLog(@"Saving context");
}
通知回调方法永远不会被执行,这让我相信这些通知从不被发布?
我的日志显示如下:
Registering for NSManagedObjectContext notifications
Saving context
...
Saving context
Doh!多么愚蠢的我:(谢谢马库斯 – nduplessis 2010-03-09 01:03:53
好赶上!我撇了过去。 – TechZen 2010-03-09 02:07:31