title: Syncing an existing Core Data store with iCloud
body: <p>I am trying to get iCloud to work with my app, which needs to migrate an existing local store to a ubiquitous store, if the user requests it.   </p>
  
  <p>After some nosing around Apple dev forums and elsewhere, I have taken this approach, which is not working consistently.  I have actually seen it work, but only after several crashes on Device B (which is populated from iCloud).</p>
  
  <pre><code>- (NSPersistentStoreCoordinator *)persistentStoreCoordinator {
    if (persistentStoreCoordinator != nil)
      return persistentStoreCoordinator;
  
    NSURL *legacyStoreUrl = [NSURL fileURLWithPath:[[self applicationDocumentsDirectory] stringByAppendingPathComponent:[self activeStoreFilenameUpgraded:NO]]];
    NSURL *upgradedStoreUrl = [NSURL fileURLWithPath:[[self applicationDocumentsDirectory] stringByAppendingPathComponent:[self activeStoreFilenameUpgraded:YES]]];
  
  persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];
  
    if ((IOS_VERSION_GREATER_THAN_OR_EQUAL_TO(@"5.0")) &amp;&amp; (self.iCloudEnabled)) {
      NSPersistentStoreCoordinator* psc = persistentStoreCoordinator;
  
      NSFileManager *fileManager = [NSFileManager defaultManager];
  
      NSDictionary *cloudOptions = nil;
      NSDictionary *localOptions = [NSDictionary dictionaryWithObjectsAndKeys:
                                      [NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption,
                                      [NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption,
                                      nil];
  
  
      NSURL *cloudURL = [fileManager URLForUbiquityContainerIdentifier:@"&lt;CONTAINER ID&gt;"];
      NSString *coreDataCloudContent = [[cloudURL path] stringByAppendingPathComponent:[NSString stringWithFormat:@"logs%d",[self activeStoreIndex]]];
      if ([coreDataCloudContent length] != 0) {
          // iCloud is available
          cloudURL = [NSURL fileURLWithPath:coreDataCloudContent];
  
          cloudOptions = [NSDictionary dictionaryWithObjectsAndKeys:
                         [NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption,
                         [NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption,
                         @"MyAppStore", NSPersistentStoreUbiquitousContentNameKey,
                         cloudURL, NSPersistentStoreUbiquitousContentURLKey,
                         nil];
      } else {
          // iCloud is not available
      }
  
      NSError *error = nil;
      [psc lock];
      if(migrateStore) {
          migrateStore = NO;
  
          NSPersistentStore *srcPS = [psc addPersistentStoreWithType:NSSQLiteStoreType
              configuration:nil
              URL:legacyStoreUrl
              options:localOptions
              error:&amp;error];
          if (![psc migratePersistentStore:srcPS
              toURL:upgradedStoreUrl
              options:cloudOptions
              withType:NSSQLiteStoreType
              error:&amp;error]) {
              NSLog(@"Error migrating data: %@, %@ / %@ / %@", error, [error userInfo], legacyStoreUrl, upgradedStoreUrl);
              abort();
          }
      }
      else {
          if (![psc addPersistentStoreWithType:NSSQLiteStoreType
              configuration:nil
              URL:upgradedStoreUrl
              options:(cloudOptions ? cloudOptions : localOptions)
              error:&amp;error]) {
                NSLog(@"Unresolved iCloud error %@, %@", error, [error userInfo]);
                abort();
          }
      }
      [psc unlock];
  
      [[NSNotificationCenter defaultCenter] postNotificationName:@"RefetchAllDatabaseData" object:self userInfo:nil];
    } else {
      NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:
                               [NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption,
                               [NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption,
                               nil];
  
      NSError *error = nil;
      if (![persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:legacyStoreUrl options:options error:&amp;error]) {
          // error
          abort();
      }    
    }
  
    return persistentStoreCoordinator;
  }
  </code></pre>