I am 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 this, I wrote the following code for my NSPersistentStoreCoordinator (of course, placeholders filled in my code):
- (NSPersistentStoreCoordinator *)persistentStoreCoordinator { if (persistentStoreCoordinator_ != nil) { return persistentStoreCoordinator_; } NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"<DB_Name>.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:@"<App_Identifier>"]; 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, @"<App_Name>.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:&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:&error]) { NSLog(@"Unresolved error %@, %@", error, [error userInfo]); } } return persistentStoreCoordinator_; }
With this code, all newly added entries are automatically synchronized between all iOS devices, so it works exactly as it should!
But I want all existing data records to be synchronized between all devices as well; this is not working yet. Existing entries are still available in the application and can be used, but they are not synchronized.
What do I need to do so that all existing data records are synchronized with iCloud? I experimented a bit with the method
migratePersistentStore:toURL:options:withType:error:
but without any success.
I am very grateful for any help!