Created
January 15, 2012 06:36
-
-
Save eric-robinson/1614732 to your computer and use it in GitHub Desktop.
Setup Core Data SQLite db for use with iCloud
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
//Some scattered methods for setting up iCloud storage of a SQLite Core Data managed database. | |
//define your ubiquity container id | |
-(NSString*)ubiquityContainerIdentifier { | |
//should match what is in your app's entitlement file | |
return [NSString stringWithFormat:@"%@.%@", @"<YOUR TEAM IDENTIFIER>", [[NSBundle mainBundle] bundleIdentifier]]; | |
} | |
//where you're going to store your database | |
- (NSURL*)ubiquitousPersistentStoreURL { | |
NSFileManager* manager = [NSFileManager defaultManager]; | |
//locate and/or create database folder | |
NSURL* containerURL = [manager URLForUbiquityContainerIdentifier:[self ubiquityContainerIdentifier]]; | |
NSString* directoryName = @"<DATABASE FOLDER NAME>.nosync"; | |
NSString* directoryURL = [containerURL URLByAppendingPathComponent:directoryName isDirectory:YES]; | |
if(![manager fileExistsAtPath:[directoryURL path]]) { | |
NSError* error = nil; | |
if(![manager createDirectoryAtURL:directoryURL withIntermediateDirectories:NO attributes:nil error:&error]) { | |
NSLog(@"error creating container directory for ubiquitous storage: %@", error); | |
return nil; | |
} | |
} | |
//append db name | |
return [[NSURL URLWithString:directoryPath] URLByAppendingPathComponent:@"<DATABASE NAME>" isDirectory:NO]; | |
} | |
//setup store in an assumed _persistentStoreCoordinator | |
-(void)addPersistentStoreToCoordinator { | |
NSMutableDictionary* storeOptions = [NSMutableDictionary dictionary]; | |
[storeOptions setObject:[NSNumber numberWithBool:YES] forKey:NSMigratePersistentStoresAutomaticallyOption]; | |
[storeOptions setObject:[NSNumber numberWithBool:YES] forKey:NSInferMappingModelAutomaticallyOption]; | |
//setup your store with iCloud options | |
[storeOptions setObject:[[NSBundle mainBundle] bundleIdentifier] forKey:NSPersistentStoreUbiquitousContentNameKey]; | |
//locate and/or create a location for iCloud to store it's transaction logs | |
NSFileManager* manager = [NSFileManager defaultManager]; | |
NSURL* containerURL = [manager URLForUbiquityContainerIdentifier:[self ubiquityContainerIdentifier]]; | |
NSString* directoryName = @"TransactionLogs";//recommended, but you can change this to whatever | |
NSURL* directoryURL = [containerURL URLByAppendingPathComponent:directoryName isDirectory:YES]; | |
if(![manager fileExistsAtPath:[directoryURL path]]) { | |
NSError* error = nil; | |
if(![manager createDirectoryAtURL:directoryURL withIntermediateDirectories:NO attributes:nil error:&error]) { | |
NSLog(@"error creating transaction logs directory for ubiquitous storage: %@", error); | |
return nil; | |
} | |
} | |
[storeOptions setObject:directoryURL forKey:NSPersistentStoreUbiquitousContentURLKey]; | |
//add store to coordinator | |
if(![_persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeUrl options:storeOptions error:&error]) { | |
//handle error | |
} | |
//listen for ubiquitous changes | |
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(ubiquitousStoreDidChange:) name:NSPersistentStoreDidImportUbiquitousContentChangesNotification object:_persistentStoreCoordinator]; | |
} | |
- (void) ubiquitousStoreDidChange:(NSNotification*)notification { | |
//new data from iCloud available, merge into our managed object context | |
[[self moc] mergeChangesFromContextDidSaveNotification:notification]; | |
NSError* error = nil; | |
[[self moc] save:&error]; | |
if(error) { | |
//something went wrong | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment