Skip to content

Commit

Permalink
Properly removing existing on-save notificaitons before replacing the…
Browse files Browse the repository at this point in the history
… default or root contexts
  • Loading branch information
blackgold9 committed Oct 9, 2012
1 parent 31b2e54 commit 343b027
Showing 1 changed file with 14 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@ + (NSManagedObjectContext *) MR_defaultContext

+ (void) MR_setDefaultContext:(NSManagedObjectContext *)moc
{
if (defaultManagedObjectContext_)
{
[[NSNotificationCenter defaultCenter] removeObserver:defaultManagedObjectContext_];
}

NSPersistentStoreCoordinator *coordinator = [NSPersistentStoreCoordinator MR_defaultStoreCoordinator];
if (iCloudSetupNotificationObserver) {
[[NSNotificationCenter defaultCenter] removeObserver:iCloudSetupNotificationObserver];
Expand Down Expand Up @@ -88,6 +93,11 @@ + (NSManagedObjectContext *) MR_rootSavingContext;

+ (void) MR_setRootSavingContext:(NSManagedObjectContext *)context;
{
if (rootSavingContext)
{
[[NSNotificationCenter defaultCenter] removeObserver:rootSavingContext];
}

rootSavingContext = context;
[context MR_obtainPermanentIDsBeforeSaving];
[rootSavingContext setMergePolicy:NSMergeByPropertyObjectTrumpMergePolicy];
Expand Down Expand Up @@ -174,8 +184,10 @@ - (void)contextWillSave:(NSNotification *)notification
NSArray *insertedObjects = [[context insertedObjects] allObjects];
MRLog(@"Context %@ is about to save. Obtaining permanent IDs for new %lu inserted objects", [context MR_description], (unsigned long)[insertedObjects count]);
NSError *error = nil;
[context obtainPermanentIDsForObjects:insertedObjects error:&error];
[MagicalRecord handleErrors:error];
BOOL success = [context obtainPermanentIDsForObjects:insertedObjects error:&error];
if (!success && error) {
[MagicalRecord handleErrors:error];
}
}
}

Expand Down

0 comments on commit 343b027

Please sign in to comment.