Skip to content

Commit fadfdb3

Browse files
committed
Merge branch 'BadModelCleanup' into development
2 parents c8ef686 + 8842d8f commit fadfdb3

File tree

5 files changed

+50
-6
lines changed

5 files changed

+50
-6
lines changed

MagicalRecord/Categories/NSPersistentStoreCoordinator+MagicalRecord.m

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -70,12 +70,30 @@ - (NSPersistentStore *) MR_addSqliteStoreNamed:(id)storeFileName withOptions:(__
7070
[self MR_createPathToStoreFileIfNeccessary:url];
7171

7272
NSPersistentStore *store = [self addPersistentStoreWithType:NSSQLiteStoreType
73-
configuration:nil
74-
URL:url
75-
options:options
76-
error:&error];
77-
if (!store)
73+
configuration:nil
74+
URL:url
75+
options:options
76+
error:&error];
77+
78+
if (!store && [MagicalRecord shouldDeleteStoreOnModelMismatch])
7879
{
80+
if ([error.domain isEqualToString:NSCocoaErrorDomain] &&
81+
[error code] == NSMigrationMissingSourceModelError) {
82+
// Could not open the database, so... kill it!
83+
[[NSFileManager defaultManager] removeItemAtURL:url error:nil];
84+
85+
// Try one more time to create the store
86+
store = [self addPersistentStoreWithType:NSSQLiteStoreType
87+
configuration:nil
88+
URL:url
89+
options:options
90+
error:&error];
91+
if (store) {
92+
// If we successfully added a store, remove the error that was initially created
93+
error = nil;
94+
}
95+
}
96+
7997
[MagicalRecord handleErrors:error];
8098
}
8199
return store;

MagicalRecord/Core/MagicalRecord+Options.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,14 @@
2020
+ (void) setShouldAutoCreateManagedObjectModel:(BOOL)shouldAutoCreate;
2121
+ (BOOL) shouldAutoCreateDefaultPersistentStoreCoordinator;
2222
+ (void) setShouldAutoCreateDefaultPersistentStoreCoordinator:(BOOL)shouldAutoCreate;
23+
+ (void) setShouldDeleteStoreOnModelMismatch:(BOOL)shouldDeleteStoreOnModelMismatch;
24+
25+
/*!
26+
@method shouldDeleteStoreOnModelMistmatch
27+
@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.
28+
This is extremely useful during development where every model change could potentially require a delete/reinstall of the app.
29+
*/
30+
+ (BOOL) shouldDeleteStoreOnModelMismatch;
2331

2432

2533
@end

MagicalRecord/Core/MagicalRecord+Options.m

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
static BOOL shouldAutoCreateManagedObjectModel_;
1212
static BOOL shouldAutoCreateDefaultPersistentStoreCoordinator_;
13+
static BOOL shouldDeleteStoreOnModelMismatch_;
1314

1415
@implementation MagicalRecord (Options)
1516

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

39+
+ (BOOL) shouldDeleteStoreOnModelMismatch;
40+
{
41+
return shouldDeleteStoreOnModelMismatch_;
42+
}
43+
44+
+ (void) setShouldDeleteStoreOnModelMismatch:(BOOL)shouldDeleteStoreOnModelMismatch
45+
{
46+
shouldDeleteStoreOnModelMismatch_ = shouldDeleteStoreOnModelMismatch;
47+
}
48+
3849
@end

MagicalRecord/Core/MagicalRecord.m

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,11 @@ + (void) initialize;
9191
#endif
9292
[self setShouldAutoCreateManagedObjectModel:YES];
9393
[self setShouldAutoCreateDefaultPersistentStoreCoordinator:NO];
94+
#ifdef DEBUG
95+
[self setShouldDeleteStoreOnModelMismatch:YES];
96+
#else
97+
[self setShouldDeleteStoreOnModelMismatch:NO];
98+
#endif
9499
}
95100
}
96101

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,9 @@ Next, somewhere in your app delegate, in either the applicationDidFinishLaunchin
8181

8282
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".
8383

84-
And, before your app exits, you can use the clean up method:
84+
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!
85+
86+
And finally, before your app exits, you can use the clean up method:
8587

8688
[MagicalRecord cleanUp];
8789

0 commit comments

Comments
 (0)