Skip to content

Commit 9e16fab

Browse files
committed
Improved error handling
- Let’s handle an addPersistentStoreWithType:configuration:URL:options:error: error regardless of whether we want to delete the store on model mismatch.
1 parent a7bfaa9 commit 9e16fab

File tree

1 file changed

+28
-26
lines changed

1 file changed

+28
-26
lines changed

MagicalRecord/Categories/NSPersistentStoreCoordinator+MagicalRecord.m

Lines changed: 28 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -75,37 +75,39 @@ - (NSPersistentStore *) MR_addSqliteStoreNamed:(id)storeFileName withOptions:(__
7575
options:options
7676
error:&error];
7777

78-
if (!store && [MagicalRecord shouldDeleteStoreOnModelMismatch])
78+
if (!store)
7979
{
80-
BOOL isMigrationError = [error code] == NSPersistentStoreIncompatibleVersionHashError || [error code] == NSMigrationMissingSourceModelError;
81-
if ([[error domain] isEqualToString:NSCocoaErrorDomain] && isMigrationError)
80+
if ([MagicalRecord shouldDeleteStoreOnModelMismatch])
8281
{
83-
// Could not open the database, so... kill it!
84-
NSFileManager *manager = [NSFileManager defaultManager];
85-
BOOL deletionResult = [NSPersistentStore MR_deleteFilesForSqliteStoreAtURL:url
86-
withFileManager:manager];
87-
88-
if (deletionResult) {
89-
MRLogInfo(@"Removed SQLite store %@ to resolve incompatible model version",
90-
[url lastPathComponent]);
91-
} else {
92-
MRLogWarn(@"Unable to fully remove SQLite store %@ to resolve incompatible model version; will try to create store one more time anyway",
93-
[url lastPathComponent]);
94-
}
95-
96-
// Try one more time to create the store
97-
store = [self addPersistentStoreWithType:NSSQLiteStoreType
98-
configuration:nil
99-
URL:url
100-
options:options
101-
error:&error];
102-
if (store)
82+
BOOL isMigrationError = [error code] == NSPersistentStoreIncompatibleVersionHashError || [error code] == NSMigrationMissingSourceModelError;
83+
if ([[error domain] isEqualToString:NSCocoaErrorDomain] && isMigrationError)
10384
{
104-
// If we successfully added a store, remove the error that was initially created
105-
error = nil;
85+
// Could not open the database, so... kill it!
86+
NSFileManager *manager = [NSFileManager defaultManager];
87+
BOOL deletionResult = [NSPersistentStore MR_deleteFilesForSqliteStoreAtURL:url
88+
withFileManager:manager];
89+
90+
if (deletionResult) {
91+
MRLogInfo(@"Removed SQLite store %@ to resolve incompatible model version",
92+
[url lastPathComponent]);
93+
} else {
94+
MRLogWarn(@"Unable to fully remove SQLite store %@ to resolve incompatible model version; will try to create store one more time anyway",
95+
[url lastPathComponent]);
96+
}
97+
98+
// Try one more time to create the store
99+
store = [self addPersistentStoreWithType:NSSQLiteStoreType
100+
configuration:nil
101+
URL:url
102+
options:options
103+
error:&error];
104+
if (store)
105+
{
106+
// If we successfully added a store, remove the error that was initially created
107+
error = nil;
108+
}
106109
}
107110
}
108-
109111
[MagicalRecord handleErrors:error];
110112
}
111113
return store;

0 commit comments

Comments
 (0)