Permalink
Browse files

Better mismatch notification coverage

  • Loading branch information...
1 parent bbbf3c3 commit 3643e690d0eff90d80f2316e3c9c54f8121aac97 @ccwasden ccwasden committed May 1, 2014
View
8 MagicalRecord/Categories/NSPersistentStoreCoordinator+MagicalRecord.h
@@ -9,8 +9,12 @@
#import "NSPersistentStore+MagicalRecord.h"
extern NSString * const kMagicalRecordPSCDidCompleteiCloudSetupNotification;
-extern NSString * const kMagicalRecordPSCWillDeleteStoreDueToModelMismatch;
-extern NSString * const kMagicalRecordPSCDidDeleteStoreDueToModelMismatch;
+extern NSString * const kMagicalRecordPSCMismatchWillDeleteStore;
+extern NSString * const kMagicalRecordPSCMismatchDidDeleteStore;
+extern NSString * const kMagicalRecordPSCMismatchWillRecreateStore;
+extern NSString * const kMagicalRecordPSCMismatchDidRecreateStore;
+extern NSString * const kMagicalRecordPSCMismatchCouldNotDeleteStore;
+extern NSString * const kMagicalRecordPSCMismatchCouldNotRecreateStore;
@interface NSPersistentStoreCoordinator (MagicalRecord)
View
26 MagicalRecord/Categories/NSPersistentStoreCoordinator+MagicalRecord.m
@@ -10,8 +10,12 @@
static NSPersistentStoreCoordinator *defaultCoordinator_ = nil;
NSString * const kMagicalRecordPSCDidCompleteiCloudSetupNotification = @"kMagicalRecordPSCDidCompleteiCloudSetupNotification";
-NSString * const kMagicalRecordPSCWillDeleteStoreDueToModelMismatch = @"kMagicalRecordPSCWillDeleteStoreDueToModelMismatch";
-NSString * const kMagicalRecordPSCDidDeleteStoreDueToModelMismatch = @"kMagicalRecordPSCDidDeleteStoreDueToModelMismatch";
+NSString * const kMagicalRecordPSCMismatchWillDeleteStore = @"kMagicalRecordPSCMismatchWillDeleteStore";
+NSString * const kMagicalRecordPSCMismatchDidDeleteStore = @"kMagicalRecordPSCMismatchDidDeleteStore";
+NSString * const kMagicalRecordPSCMismatchWillRecreateStore = @"kMagicalRecordPSCMismatchWillRecreateStore";
+NSString * const kMagicalRecordPSCMismatchDidRecreateStore = @"kMagicalRecordPSCMismatchDidRecreateStore";
+NSString * const kMagicalRecordPSCMismatchCouldNotDeleteStore = @"kMagicalRecordPSCMismatchCouldNotDeleteStore";
+NSString * const kMagicalRecordPSCMismatchCouldNotRecreateStore = @"kMagicalRecordPSCMismatchCouldNotRecreateStore";
@interface NSDictionary (MagicalRecordMerging)
@@ -85,17 +89,26 @@ - (NSPersistentStore *) MR_addSqliteStoreNamed:(id)storeFileName withOptions:(__
BOOL isMigrationError = (([error code] == NSPersistentStoreIncompatibleVersionHashError) || ([error code] == NSMigrationMissingSourceModelError));
if ([[error domain] isEqualToString:NSCocoaErrorDomain] && isMigrationError)
{
- [[NSNotificationCenter defaultCenter] postNotificationName:kMagicalRecordPSCWillDeleteStoreDueToModelMismatch object:nil];
+ [[NSNotificationCenter defaultCenter] postNotificationName:kMagicalRecordPSCMismatchWillDeleteStore object:nil];
+
+ NSError * deleteStoreError;
// Could not open the database, so... kill it! (AND WAL bits)
NSString *rawURL = [url absoluteString];
NSURL *shmSidecar = [NSURL URLWithString:[rawURL stringByAppendingString:@"-shm"]];
NSURL *walSidecar = [NSURL URLWithString:[rawURL stringByAppendingString:@"-wal"]];
- [[NSFileManager defaultManager] removeItemAtURL:url error:nil];
+ [[NSFileManager defaultManager] removeItemAtURL:url error:&deleteStoreError];
[[NSFileManager defaultManager] removeItemAtURL:shmSidecar error:nil];
[[NSFileManager defaultManager] removeItemAtURL:walSidecar error:nil];
MRLogWarn(@"Removed incompatible model version: %@", [url lastPathComponent]);
+ if(deleteStoreError) {
+ [[NSNotificationCenter defaultCenter] postNotificationName:kMagicalRecordPSCMismatchCouldNotDeleteStore object:deleteStoreError];
+ }
+ else {
+ [[NSNotificationCenter defaultCenter] postNotificationName:kMagicalRecordPSCMismatchDidDeleteStore object:nil];
+ }
+ [[NSNotificationCenter defaultCenter] postNotificationName:kMagicalRecordPSCMismatchWillRecreateStore object:nil];
// Try one more time to create the store
store = [self addPersistentStoreWithType:NSSQLiteStoreType
configuration:nil
@@ -104,10 +117,13 @@ - (NSPersistentStore *) MR_addSqliteStoreNamed:(id)storeFileName withOptions:(__
error:&error];
if (store)
{
- [[NSNotificationCenter defaultCenter] postNotificationName:kMagicalRecordPSCDidDeleteStoreDueToModelMismatch object:nil];
+ [[NSNotificationCenter defaultCenter] postNotificationName:kMagicalRecordPSCMismatchDidRecreateStore object:nil];
// If we successfully added a store, remove the error that was initially created
error = nil;
}
+ else {
+ [[NSNotificationCenter defaultCenter] postNotificationName:kMagicalRecordPSCMismatchCouldNotRecreateStore object:error];
+ }
}
}
[MagicalRecord handleErrors:error];

0 comments on commit 3643e69

Please sign in to comment.