Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

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.
  • Loading branch information...
commit 9e16fab85254cc1fb7572a03dfbed8e0e9a3c027 1 parent a7bfaa9
@bachand bachand authored
Showing with 28 additions and 26 deletions.
  1. +28 −26 MagicalRecord/Categories/NSPersistentStoreCoordinator+MagicalRecord.m
View
54 MagicalRecord/Categories/NSPersistentStoreCoordinator+MagicalRecord.m
@@ -75,37 +75,39 @@ - (NSPersistentStore *) MR_addSqliteStoreNamed:(id)storeFileName withOptions:(__
options:options
error:&error];
- if (!store && [MagicalRecord shouldDeleteStoreOnModelMismatch])
+ if (!store)
{
- BOOL isMigrationError = [error code] == NSPersistentStoreIncompatibleVersionHashError || [error code] == NSMigrationMissingSourceModelError;
- if ([[error domain] isEqualToString:NSCocoaErrorDomain] && isMigrationError)
+ if ([MagicalRecord shouldDeleteStoreOnModelMismatch])
{
- // Could not open the database, so... kill it!
- NSFileManager *manager = [NSFileManager defaultManager];
- BOOL deletionResult = [NSPersistentStore MR_deleteFilesForSqliteStoreAtURL:url
- withFileManager:manager];
-
- if (deletionResult) {
- MRLogInfo(@"Removed SQLite store %@ to resolve incompatible model version",
- [url lastPathComponent]);
- } else {
- MRLogWarn(@"Unable to fully remove SQLite store %@ to resolve incompatible model version; will try to create store one more time anyway",
- [url lastPathComponent]);
- }
-
- // Try one more time to create the store
- store = [self addPersistentStoreWithType:NSSQLiteStoreType
- configuration:nil
- URL:url
- options:options
- error:&error];
- if (store)
+ BOOL isMigrationError = [error code] == NSPersistentStoreIncompatibleVersionHashError || [error code] == NSMigrationMissingSourceModelError;
+ if ([[error domain] isEqualToString:NSCocoaErrorDomain] && isMigrationError)
{
- // If we successfully added a store, remove the error that was initially created
- error = nil;
+ // Could not open the database, so... kill it!
+ NSFileManager *manager = [NSFileManager defaultManager];
+ BOOL deletionResult = [NSPersistentStore MR_deleteFilesForSqliteStoreAtURL:url
+ withFileManager:manager];
+
+ if (deletionResult) {
+ MRLogInfo(@"Removed SQLite store %@ to resolve incompatible model version",
+ [url lastPathComponent]);
+ } else {
+ MRLogWarn(@"Unable to fully remove SQLite store %@ to resolve incompatible model version; will try to create store one more time anyway",
+ [url lastPathComponent]);
+ }
+
+ // 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;
Please sign in to comment.
Something went wrong with that request. Please try again.