Created
September 19, 2012 21:16
-
-
Save mlaster/3752316 to your computer and use it in GitHub Desktop.
Handling unreadable CoreData storage
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- (NSPersistentStoreCoordinator *)persistentStoreCoordinator { | |
if (__persistentStoreCoordinator != nil) { | |
return __persistentStoreCoordinator; | |
} | |
NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"database.sqlite"]; | |
NSError *error = nil; | |
@try { | |
__persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:self.managedObjectModel]; | |
} @catch (NSException *e) { | |
NSLog(@"EXCEPTION: %@", e); | |
} | |
if ([__persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:nil error:&error] == nil) { | |
NSLog(@"Deleting unreadable %@", storeURL); | |
error = nil; | |
[[NSFileManager defaultManager] removeItemAtURL:storeURL error:&error]; | |
if (error != nil) { | |
NSLog(@"Unable to delete %@\n%@\n%@", storeURL, error, [error userInfo]); | |
abort(); | |
} | |
if ([self.persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:nil error:&error] == nil) { | |
NSLog(@"Unresolved error %@, %@", error, [error userInfo]); | |
abort(); | |
} | |
} | |
return __persistentStoreCoordinator; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment