-(void)someBackgroundTask {
NSManagedObjectContext *context = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSConfinementConcurrencyType];
[context setPersistentStoreCoordinator:[self coordinator]];
// ...
NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter];
[notificationCenter addObserver:self selector:@selector(handleSaveNotification:) name:NSManagedObjectContextDidSaveNotification object:context];
[context save:&error];
// safe?
[notificationCenter removeObserver:self name:NSManagedObjectContextDidSaveNotification object:context];
// ...
}
// meanwhile on the main thread...
-(void)handleSaveNotification:(NSNotification *)notification {
[[self context] mergeChangesFromContextDidSaveNotification:notification];
}
在调用 to 之后这么快就移除观察者是否安全save:
?