Skip to content

Instantly share code, notes, and snippets.

@ap4y
Created March 30, 2014 22:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ap4y/9881152 to your computer and use it in GitHub Desktop.
Save ap4y/9881152 to your computer and use it in GitHub Desktop.
NSPersistenceStoreCoordinator with SQLStore
+ (BOOL)addInSQLStorageWithName:(NSString *)dbFileName error:(NSError **)error {
if (!defaultCoordinator) {
if (error) {
NSDictionary *errorInfo = @{
NSLocalizedDescriptionKey: NSLocalizedString(@"Default coordinator is not registered.", nil)
};
*error = [NSError errorWithDomain:NSCocoaErrorDomain
code:NSCoreDataError
userInfo:errorInfo];
}
return NO;
}
NSDictionary *options = @{
NSMigratePersistentStoresAutomaticallyOption: [NSNumber numberWithBool:YES],
NSInferMappingModelAutomaticallyOption: [NSNumber numberWithBool:YES]
};
NSString *fileName = [NSString stringWithFormat:@"%@.sqlite", dbFileName];
NSArray *pathes = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
NSString *appPath = [pathes lastObject];
appPath = [appPath stringByAppendingPathComponent:fileName];
NSURL *storeURL = [NSURL fileURLWithPath:appPath isDirectory:NO];
return [defaultCoordinator addPersistentStoreWithType:NSSQLiteStoreType
configuration:nil
URL:storeURL
options:options
error:error];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment