Note that there are some explanatory texts on larger screens.

plurals
  1. POLightweight migration with iCloud
    text
    copied!<p>I have an app that can be run two ways: Using Local Storage or using iCloud. When I use local storage, my lightweight migration works fine, but when I choose iCloud, it causing all my data to be lost. Here is the code I use to initialize the persistent store coordinator:</p> <pre><code>NSMutableDictionary *options = [NSMutableDictionary dictionary]; NSFileManager *fm = [NSFileManager defaultManager]; NSURL *ubContainer = [fm URLForUbiquityContainerIdentifier:nil]; NSURL *logsDir = [ubContainer URLByAppendingPathComponent:@"logsDir"]; [options setObject:[NSNumber numberWithBool:YES] forKey:NSMigratePersistentStoresAutomaticallyOption]; [options setObject:[NSNumber numberWithBool:YES] forKey:NSInferMappingModelAutomaticallyOption]; if ([[NSUserDefaults standardUserDefaults] boolForKey:kUserDefaultsUseICloud]) { // Construct the dictionary that tells Core Data where the transaction log should be stored [options setObject:@"comcompanyappname" forKey:NSPersistentStoreUbiquitousContentNameKey]; [options setObject:logsDir forKey:NSPersistentStoreUbiquitousContentURLKey]; } // Read in xcdatamodeld model = [NSManagedObjectModel mergedModelFromBundles:nil]; psc = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:model]; NSURL *nosyncDir = [ubContainer URLByAppendingPathComponent:@"appname.nosync"]; NSURL *storeURL; if (ubContainer) { [fm createDirectoryAtURL:nosyncDir withIntermediateDirectories:YES attributes:nil error:nil]; storeURL = [nosyncDir URLByAppendingPathComponent:@"store.data"]; } else{ NSString *path = [self itemArchivePath]; storeURL = [NSURL fileURLWithPath:path]; } NSError *error = nil; [psc lock]; if (![psc addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:options error:&amp;error]) { [NSException raise:@"Open failed" format:@"Reason: %@", [error localizedDescription]]; } [psc unlock]; // Create the managed object context context = [[NSManagedObjectContext alloc] init]; [context setPersistentStoreCoordinator:psc]; [context setMergePolicy:NSMergeByPropertyObjectTrumpMergePolicy]; // The managed object context can manage undo, but we don't need it [context setUndoManager:nil]; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload