title: iOS: Migrating existing Core Data-database into iCloud
body: <p>I'm using Core Data in an existing application. Now I want to integrate iCloud so that the user can synchronize their contents between their iOS-devices. To do that I've written the following code for my NSPersistentStoreCoordinator (of course the placeholders are filled out in my code):</p>
  
  <pre><code>- (NSPersistentStoreCoordinator *)persistentStoreCoordinator {
  
  if (persistentStoreCoordinator_ != nil) {
      return persistentStoreCoordinator_;
  }
  
  NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"&lt;DB_Name&gt;.sqlite"];
  
  persistentStoreCoordinator_ = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];
  
  NSPersistentStoreCoordinator* psc = persistentStoreCoordinator_;
  
  if (IOS_VERSION_GREATER_THAN_OR_EQUAL_TO(@"5.0")) {
      dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
          NSFileManager *fileManager = [NSFileManager defaultManager];
  
          // Migrate datamodel
          NSDictionary *options = nil;
  
          // this needs to match the entitlements and provisioning profile
          NSURL *cloudURL = [fileManager URLForUbiquityContainerIdentifier:@"&lt;App_Identifier&gt;"];
          NSString* coreDataCloudContent = [[cloudURL path] stringByAppendingPathComponent:@"data"];
          if ([coreDataCloudContent length] != 0) {
              // iCloud is available
              cloudURL = [NSURL fileURLWithPath:coreDataCloudContent];
  
              options = [NSDictionary dictionaryWithObjectsAndKeys:
                         [NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption,
                         [NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption,
                         @"&lt;App_Name&gt;.store", NSPersistentStoreUbiquitousContentNameKey,
                         cloudURL, NSPersistentStoreUbiquitousContentURLKey,
                         nil];
          } else {
              // iCloud is not available
              options = [NSDictionary dictionaryWithObjectsAndKeys:
                         [NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption,
                         [NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption,
                         nil];
          }
  
          NSError *error = nil;
          [psc lock];
          if (![psc addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:options error:&amp;error])
          {
              NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
          }
          [psc unlock];
  
          dispatch_async(dispatch_get_main_queue(), ^{
              NSLog(@"asynchronously added persistent store!");
              [[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:storeURL options:options error:&amp;error])
      {
          NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
      }
  }
  
  return persistentStoreCoordinator_;
  }
  </code></pre>
  
  <p>With that code all <strong>new added</strong> data records are synchronized automatically between all iOS-devices, so that works exactly the way it should!</p>
  
  <p>But what I want is that also <strong>all the existing</strong> data records are synced between all devices; that doesn't work yet. The existing records are still available within the app and can be used, but they are not synchronized.</p>
  
  <p>What do I need to do to get all the existing data records synced with iCloud too? I've experimented a bit with the method</p>
  
  <pre><code>migratePersistentStore:toURL:options:withType:error:
  </code></pre>
  
  <p>but without any success.</p>
  
  <p>I'm very grateful for any help!</p>