Skip to content

Commit

Permalink
Added automatic store deletion if the store does not match the model
Browse files Browse the repository at this point in the history
  • Loading branch information
blackgold9 committed Aug 20, 2012
1 parent acff79a commit b8326a6
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,31 @@ - (NSPersistentStore *) MR_addSqliteStoreNamed:(id)storeFileName withOptions:(__
[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;
Expand Down
8 changes: 8 additions & 0 deletions MagicalRecord/Core/MagicalRecord+Options.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
11 changes: 11 additions & 0 deletions MagicalRecord/Core/MagicalRecord+Options.m
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

static BOOL shouldAutoCreateManagedObjectModel_;
static BOOL shouldAutoCreateDefaultPersistentStoreCoordinator_;
static BOOL shouldDeleteStoreOnModelMismatch_;

@implementation MagicalRecord (Options)

Expand All @@ -35,4 +36,14 @@ + (void) setShouldAutoCreateDefaultPersistentStoreCoordinator:(BOOL)shouldAutoCr
shouldAutoCreateDefaultPersistentStoreCoordinator_ = shouldAutoCreate;
}

+ (BOOL) shouldDeleteStoreOnModelMismatch;
{
return shouldDeleteStoreOnModelMismatch_;
}

+ (void) setShouldDeleteStoreOnModelMismatch:(BOOL)shouldDeleteStoreOnModelMismatch
{
shouldDeleteStoreOnModelMismatch_ = shouldDeleteStoreOnModelMismatch;
}

@end

0 comments on commit b8326a6

Please sign in to comment.