Created
August 2, 2010 16:00
-
-
Save andrewsardone/504848 to your computer and use it in GitHub Desktop.
Automatically deleting our persistent store upon model changes in early development of @nutshellcrm
This file contains 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
#define kIncompatibleModelErrorReason @"The model used to open the store is incompatible with the one used to create the store" | |
/** | |
Returns the persistent store coordinator for the application. | |
If the coordinator doesn't already exist, it is created and the application's store added to it. | |
*/ | |
- (NSPersistentStoreCoordinator *)persistentStoreCoordinator { | |
if (persistentStoreCoordinator_ != nil) { | |
return persistentStoreCoordinator_; | |
} | |
NSURL *storeUrl = [NSURL fileURLWithPath: [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:kStoreName]]; | |
NSError *error = nil; | |
persistentStoreCoordinator_ = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:self.managedObjectModel]; | |
if (![persistentStoreCoordinator_ addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeUrl options:nil error:&error]) { | |
if ([[[error userInfo] objectForKey:@"reason"] isEqualToString:kIncompatibleModelErrorReason]) { | |
NSFileManager *fileManager = [NSFileManager defaultManager]; | |
if ([fileManager removeItemAtURL:storeUrl error:NULL]) { | |
persistentStoreCoordinator_ = nil; | |
return [self persistentStoreCoordinator]; | |
} | |
} else { | |
/** | |
* Replace this implementation with code to handle other errors appropriately. | |
* | |
* abort() causes the application to generate a crash log and terminate. You should not use | |
* this function in a shipping application, although it may be useful during development. If | |
* it is not possible to recover from the error, display an alert panel that instructs the | |
* user to quit the application by pressing the Home button. | |
*/ | |
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