开发者

Missing Core Data Notifications

I'm trying a multithreaded Core Data implementation on iPhone SDK 3.1.3. I have two different NSManagedObjectContext objects for each thread and I am registering for the change notifications on one thread like below:

- (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");
}

In my o开发者_Python百科ther thread I am saving the second context like so:

NSError *error = nil;
[managedObjectContext save:&error];

if (error) {
    ALog(@"Error occured while trying to save a NewsStory object");
}
else {
    DLog(@"Saving context");
}

The notification callback method never gets executed though, which makes me believe these notifications are never posted?

My log shows the following:

Registering for NSManagedObjectContext notifications
Saving context
...
Saving context


Switch your @"NSManagedObjectContextWillSaveNotification" to NSManagedObjectContextWillSaveNotification. Those are constants. The events are most likely being posted but you are not listening for the right name.


One thing I know causes problems with notifications and Core Data is having a custom managed object subclass that is not properly key-value observing compliant. This prevents the change notifications from being created in the first place.

As a test, I would suggest registering an observer in the same thread or even having the subclass (if you use them) register to observe themselves. If that doesn't work then you problem is more basic than the notifications. If it does work, then your problem is in your threading.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜