IOS: Migrating an Existing Core Data Database to iCloud

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!

+6
source share
2 answers

See a WWDC 2012 session. Using CoreData with iCloud. They discuss this in the final section on sowing. They also have sample code that you can get from a site that has an example. In short, you will need to open 2 permanent stores, set the sample size, then take batches from the source, skip them and add them to the new store, as well as at batch size intervals, save and reset. Pretty simple.

+1
source

Check out WWDC 2012 - Use master data with iCloud. This method is called sowing. Find the sample code https://github.com/jab5990/TestCDiCloud/tree/master/Shared

0
source

Source: https://habr.com/ru/post/914193/


All Articles