Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Cleanup contextWillSave method

Update deleting incompatible store
  • Loading branch information...
commit 2eaec274103bfc265b6369b284c189d39e37367f 1 parent abb7314
@casademora casademora authored
View
17 MagicalRecord/Categories/NSManagedObjectContext/NSManagedObjectContext+MagicalRecord.m
@@ -162,19 +162,22 @@ + (NSManagedObjectContext *) MR_contextWithStoreCoordinator:(NSPersistentStoreCo
- (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];
}
}
View
9 MagicalRecord/Categories/NSPersistentStoreCoordinator+MagicalRecord.m
@@ -77,10 +77,12 @@ - (NSPersistentStore *) MR_addSqliteStoreNamed:(id)storeFileName withOptions:(__
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 @@ - (NSPersistentStore *) MR_addSqliteStoreNamed:(id)storeFileName withOptions:(__
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;
}

0 comments on commit 2eaec27

Please sign in to comment.
Something went wrong with that request. Please try again.