diff --git a/MagicalRecord/Categories/NSPersistentStoreCoordinator+MagicalRecord.m b/MagicalRecord/Categories/NSPersistentStoreCoordinator+MagicalRecord.m index 5c5f56b..66b8da7 100644 --- a/MagicalRecord/Categories/NSPersistentStoreCoordinator+MagicalRecord.m +++ b/MagicalRecord/Categories/NSPersistentStoreCoordinator+MagicalRecord.m @@ -70,12 +70,30 @@ NSString * const kMagicalRecordPSCDidCompleteiCloudSetupNotification = @"kMagica [self MR_createPathToStoreFileIfNeccessary:url]; NSPersistentStore *store = [self addPersistentStoreWithType:NSSQLiteStoreType - configuration:nil - URL:url - options:options - error:&error]; - if (!store) + configuration:nil + URL:url + options:options + error:&error]; + + if (!store && [MagicalRecord shouldDeleteStoreOnModelMismatch]) { + if ([error.domain isEqualToString:NSCocoaErrorDomain] && + [error code] == NSMigrationMissingSourceModelError) { + // Could not open the database, so... kill it! + [[NSFileManager defaultManager] removeItemAtURL:url error:nil]; + + // Try one more time to create the store + store = [self addPersistentStoreWithType:NSSQLiteStoreType + configuration:nil + URL:url + options:options + error:&error]; + if (store) { + // If we successfully added a store, remove the error that was initially created + error = nil; + } + } + [MagicalRecord handleErrors:error]; } return store; diff --git a/MagicalRecord/Core/MagicalRecord+Options.h b/MagicalRecord/Core/MagicalRecord+Options.h index e0d224e..72012c7 100644 --- a/MagicalRecord/Core/MagicalRecord+Options.h +++ b/MagicalRecord/Core/MagicalRecord+Options.h @@ -20,6 +20,14 @@ + (void) setShouldAutoCreateManagedObjectModel:(BOOL)shouldAutoCreate; + (BOOL) shouldAutoCreateDefaultPersistentStoreCoordinator; + (void) setShouldAutoCreateDefaultPersistentStoreCoordinator:(BOOL)shouldAutoCreate; ++ (void) setShouldDeleteStoreOnModelMismatch:(BOOL)shouldDeleteStoreOnModelMismatch; + +/*! + @method shouldDeleteStoreOnModelMistmatch + @abstract If true, when configuring the persistant store coordinator, and Magical Record encounters a store that does not match the model, it will attempt to remove it and re-create a new store. + This is extremely useful during development where every model change could potentially require a delete/reinstall of the app. + */ ++ (BOOL) shouldDeleteStoreOnModelMismatch; @end diff --git a/MagicalRecord/Core/MagicalRecord+Options.m b/MagicalRecord/Core/MagicalRecord+Options.m index eb48c80..a076558 100644 --- a/MagicalRecord/Core/MagicalRecord+Options.m +++ b/MagicalRecord/Core/MagicalRecord+Options.m @@ -10,6 +10,7 @@ static BOOL shouldAutoCreateManagedObjectModel_; static BOOL shouldAutoCreateDefaultPersistentStoreCoordinator_; +static BOOL shouldDeleteStoreOnModelMismatch_; @implementation MagicalRecord (Options) @@ -35,4 +36,14 @@ static BOOL shouldAutoCreateDefaultPersistentStoreCoordinator_; shouldAutoCreateDefaultPersistentStoreCoordinator_ = shouldAutoCreate; } ++ (BOOL) shouldDeleteStoreOnModelMismatch; +{ + return shouldDeleteStoreOnModelMismatch_; +} + ++ (void) setShouldDeleteStoreOnModelMismatch:(BOOL)shouldDeleteStoreOnModelMismatch +{ + shouldDeleteStoreOnModelMismatch_ = shouldDeleteStoreOnModelMismatch; +} + @end diff --git a/MagicalRecord/Core/MagicalRecord.m b/MagicalRecord/Core/MagicalRecord.m index 7be05e0..658b6cd 100644 --- a/MagicalRecord/Core/MagicalRecord.m +++ b/MagicalRecord/Core/MagicalRecord.m @@ -91,6 +91,11 @@ void reset_action_queue(void); #endif [self setShouldAutoCreateManagedObjectModel:YES]; [self setShouldAutoCreateDefaultPersistentStoreCoordinator:NO]; +#ifdef DEBUG + [self setShouldDeleteStoreOnModelMismatch:YES]; +#else + [self setShouldDeleteStoreOnModelMismatch:NO]; +#endif } } diff --git a/README.md b/README.md index 399a374..29cbad6 100644 --- a/README.md +++ b/README.md @@ -81,7 +81,9 @@ Next, somewhere in your app delegate, in either the applicationDidFinishLaunchin Each call instantiates one of each piece of the Core Data stack, and provides getter and setter methods for these instances. These well known instances to MagicalRecord, and are recognized as "defaults". -And, before your app exits, you can use the clean up method: +When using the default sqlite data store with the DEBUG flag set, if you change your model without creating a new model version, Magical Record will delete the old store and create a new one automatically. No more uninstall/reinstall every time you make a change! + +And finally, before your app exits, you can use the clean up method: [MagicalRecord cleanUp];