Skip to content

Instantly share code, notes, and snippets.

@egrim
Last active December 26, 2015 20:29
Show Gist options
  • Save egrim/7209519 to your computer and use it in GitHub Desktop.
Save egrim/7209519 to your computer and use it in GitHub Desktop.
- (NSPersistentStore *) MR_addSqliteStoreNamed:(id)storeFileName withOptions:(__autoreleasing NSDictionary *)options
{
NSURL *url = [storeFileName isKindOfClass:[NSURL class]] ? storeFileName : [NSPersistentStore MR_urlForStoreName:storeFileName];
NSError *error = nil;
[self MR_createPathToStoreFileIfNeccessary:url];
// NSPersistentStore *store = [self addPersistentStoreWithType:NSSQLiteStoreType
// configuration:nil
// URL:url
// options:options
// error:&error];
NSPersistentStore *store = nil;
error = [NSError errorWithDomain:NSCocoaErrorDomain code:256 userInfo:nil];
if (!store && [MagicalRecord shouldDeleteStoreOnModelMismatch])
{
BOOL isMigrationError = [error code] == NSPersistentStoreIncompatibleVersionHashError || [error code] == NSMigrationMissingSourceModelError;
if ([[error domain] isEqualToString:NSCocoaErrorDomain] && isMigrationError)
{
// Could not open the database, so... kill it!
[[NSFileManager defaultManager] removeItemAtURL:url error:nil];
MRLog(@"Removed incompatible model version: %@", [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;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment