简体   繁体   English

您如何将现有的核心数据iOS 7应用程序的数据迁移到iCloud?

[英]How do you migrate an existing core data iOS 7 app's data into iCloud?

I have an iOS 7 app that already uses Core Data. 我有一个已经使用Core Data的iOS 7应用。 I have used the new iOS 7 method of integrating iCloud into my app to sync items stored in core data by using the following code as an example: 我已使用新的iOS 7方法将iCloud集成到我的应用程序中,以使用以下代码为例同步存储在核心数据中的项目:

https://github.com/mluisbrown/iCloudCoreDataStack/blob/master/README.md https://github.com/mluisbrown/iCloudCoreDataStack/blob/master/README.md

This works great, except that all of the original data on the device doesn't show up in the iCloud store. 这很好用,只是设备上的所有原始数据没有显示在iCloud存储中。 I keep hearing that I need to migrate the data - but I can't find any examples on how to do this properly. 我一直听到我需要迁移数据-但找不到有关如何正确执行此操作的任何示例。 Does anyone know how to do this? 有谁知道如何做到这一点?

I keep getting pointed to using migratePersistentStore:toURL:options:withType:error: , but I don't see how I can use this... 我一直指着使用migrationPersistentStore:toURL:options:withType:error:,但是我看不到如何使用它...

Here is a sample app with a iCloud control panel to move the store to or from iCloud. 这是带有iCloud控制面板的示例应用程序,用于将商店移入或移出iCloud。 To move your existing store you need to open it with the existing options but make sure you use iOS7 options for the target store. 要移动现有商店,您需要使用现有选项将其打开,但请确保对目标商店使用iOS7选项。 Take a look at the sample apps code in OSCDStackManager and if you have specific questions then post them. 查看OSCDStackManager中的示例应用程序代码,如果您有特定问题,请发布它们。 http://ossh.com.au/design-and-technology/software-development/sample-library-style-ios-core-data-app-with-icloud-integration/ http://ossh.com.au/design-and-technology/software-development/sample-library-style-ios-core-data-app-with-icloud-integration/

- (bool)moveStoreFileToICloud:(NSURL*)fileURL delete:(bool)shouldDelete backup:(bool)shouldBackup {
    FLOG(@" called");

    // Always make a backup of the local store before migrating to iCloud
    if (shouldBackup)
        [self backupLocalStore];

    NSPersistentStoreCoordinator *migrationPSC = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:self.managedObjectModel];

    // Open the existing local store using the original options
    id sourceStore = [migrationPSC addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:fileURL options:[self localStoreOptions] error:nil];

    if (!sourceStore) {

        FLOG(@" failed to add old store");
        return FALSE;
    } else {
        FLOG(@" Successfully added store to migrate");

        bool moveSuccess = NO;
        NSError *error;

        FLOG(@" About to migrate the store...");
        // Now migrate the store using the iCloud options
        id migrationSuccess = [migrationPSC migratePersistentStore:sourceStore toURL:[self icloudStoreURL] options:[self icloudStoreOptions] withType:NSSQLiteStoreType error:&error];

        if (migrationSuccess) {
            moveSuccess = YES;
            FLOG(@"store successfully migrated");
            [self deregisterForStoreChanges];
            _persistentStoreCoordinator = nil;
            _managedObjectContext = nil;
            self.storeURL = [self icloudStoreURL];
            // Now delete the local file
            if (shouldDelete) {
                FLOG(@" deleting local store");
                [self deleteLocalStore];
            } else {
                FLOG(@" not deleting local store");
            }
            return TRUE;
        }
        else {
            FLOG(@"Failed to migrate store: %@, %@", error, error.userInfo);
            return FALSE;
        }

    }
    return FALSE;
}

You move your existing store to a different path, and then you call the migrate method with the toURL set to the path where you want your store to end up. 您将现有商店移动到其他路径,然后调用toURL设置为您希望商店结束的路径的toURL方法。

You need to pass the options in that include the ubiquity settings that an iCloud store needs to have set. 您需要传递其中包括iCloud存储区需要设置的普遍设置的选项。

When the migration is finished, you should have two copies of the store: the non-iCloud one which you moved aside, and the new one with iCloud options set. 迁移完成后,您应该有两个商店副本:非iCloud副本(已移开)和新副本(已设置iCloud选项)。 You can now remove the old store if you like, and just setup your Core Data stack to use the iCloud store. 现在,您可以根据需要删除旧存储,只需设置Core Data堆栈即可使用iCloud存储。

Take a look at some of the methods in this example . 看一下此示例中的一些方法。 In particular, look at the ones beginning with 'migrate'. 特别要注意的是那些以“ migrate”开头的内容。 You should be able to work out what steps to take to migrate data to a new cloud store. 您应该能够确定将数据迁移到新的云存储所要采取的步骤。

Core Data sync is hard to get right, especially when you start to get into migrating in data. 核心数据同步很难正确处理,尤其是当您开始着手进行数据迁移时。 It is worth looking at other Core Data sync options like Wasabi Sync and Ensembles . 值得研究其他核心数据同步选项,例如Wasabi SyncEnsembles They handle migration and merging of data automatically. 它们自动处理数据的迁移和合并。 (Disclosure: I develop Ensembles) (公开:我开发了乐团)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM