iOS: Migracja istniejącej bazy danych Core Data do iCloud

Używam danych podstawowych w istniejącej aplikacji. Teraz chcę zintegrować iCloud, aby użytkownik mógł zsynchronizować swoją zawartość między swoimi urządzeniami iOS. W tym celu napisałem następujący kod dla mojego NSPersistentStoreCoordinator (oczywiście symbole zastępcze są wypełniane w moim kodzie):

<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_;
}
</code>

Z tym kodem wszystkonowe dodane rekordy danych są automatycznie synchronizowane między wszystkimi urządzeniami z systemem iOS, więc działa dokładnie tak, jak powinno!

Ale ja też tego chcęwszystkie istniejące rekordy danych są synchronizowane między wszystkimi urządzeniami; to jeszcze nie działa. Istniejące rekordy są nadal dostępne w aplikacji i mogą być używane, ale nie są synchronizowane.

Co muszę zrobić, aby zsynchronizować wszystkie istniejące rekordy danych z iCloud? Eksperymentowałem trochę z tą metodą

<code>migratePersistentStore:toURL:options:withType:error:
</code>

ale bez powodzenia.

Jestem bardzo wdzięczny za wszelką pomoc!

questionAnswers(2)

yourAnswerToTheQuestion