Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 other 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
like image 929
nduplessis Avatar asked Dec 29 '25 19:12

nduplessis


1 Answers

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

like image 113
Marcus S. Zarra Avatar answered Jan 01 '26 13:01

Marcus S. Zarra