diff --git a/MagicalRecord/Categories/NSManagedObjectContext/NSManagedObjectContext+MagicalRecord.m b/MagicalRecord/Categories/NSManagedObjectContext/NSManagedObjectContext+MagicalRecord.m index abc8e4f..c2c5263 100644 --- a/MagicalRecord/Categories/NSManagedObjectContext/NSManagedObjectContext+MagicalRecord.m +++ b/MagicalRecord/Categories/NSManagedObjectContext/NSManagedObjectContext+MagicalRecord.m @@ -162,19 +162,22 @@ static id iCloudSetupNotificationObserver = nil; - (void) MR_obtainPermanentIDsBeforeSaving; { [[NSNotificationCenter defaultCenter] addObserver:self - selector:@selector(contextWillSave:) + selector:@selector(MR_contextWillSave:) name:NSManagedObjectContextWillSaveNotification object:self]; } -- (void)contextWillSave:(NSNotification *)notification +- (void) MR_contextWillSave:(NSNotification *)notification { - NSManagedObjectContext *context = (NSManagedObjectContext *)notification.object; - if (context.insertedObjects.count > 0) { - NSArray *insertedObjects = [[context insertedObjects] allObjects]; + NSManagedObjectContext *context = [notification object]; + NSSet *insertedObjects = [context insertedObjects]; + + if ([insertedObjects count]) + { MRLog(@"Context %@ is about to save. Obtaining permanent IDs for new %lu inserted objects", [context MR_description], (unsigned long)[insertedObjects count]); - NSError *error; - [context obtainPermanentIDsForObjects:insertedObjects error:&error]; + + NSError *error = nil; + [context obtainPermanentIDsForObjects:[insertedObjects allObjects] error:&error]; [MagicalRecord handleErrors:error]; } } diff --git a/MagicalRecord/Categories/NSPersistentStoreCoordinator+MagicalRecord.m b/MagicalRecord/Categories/NSPersistentStoreCoordinator+MagicalRecord.m index eaf9fc9..05047ab 100644 --- a/MagicalRecord/Categories/NSPersistentStoreCoordinator+MagicalRecord.m +++ b/MagicalRecord/Categories/NSPersistentStoreCoordinator+MagicalRecord.m @@ -77,10 +77,12 @@ NSString * const kMagicalRecordPSCDidCompleteiCloudSetupNotification = @"kMagica if (!store && [MagicalRecord shouldDeleteStoreOnModelMismatch]) { - if ([error.domain isEqualToString:NSCocoaErrorDomain] && - [error code] == NSMigrationMissingSourceModelError) { + if ([[error domain] isEqualToString:NSCocoaErrorDomain] && [error code] == NSPersistentStoreIncompatibleVersionHashError) + { // Could not open the database, so... kill it! [[NSFileManager defaultManager] removeItemAtURL:url error:nil]; + + MRLog(@"Removed incompatible model version: %@", [url lastPathComponent]); // Try one more time to create the store store = [self addPersistentStoreWithType:NSSQLiteStoreType @@ -88,7 +90,8 @@ NSString * const kMagicalRecordPSCDidCompleteiCloudSetupNotification = @"kMagica URL:url options:options error:&error]; - if (store) { + if (store) + { // If we successfully added a store, remove the error that was initially created error = nil; }