【问题标题】:How do you migrate an existing core data iOS 7 app's data into iCloud?如何将现有核心数据 iOS 7 应用程序的数据迁移到 iCloud?
【发布时间】:2014-02-28 19:42:05
【问题描述】:

我有一个已经使用 Core Data 的 iOS 7 应用。我已经使用新的 iOS 7 方法将 iCloud 集成到我的应用程序中,以同步存储在核心数据中的项目,使用以下代码作为示例:

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

这很好用,只是设备上的所有原始数据都不会显示在 iCloud 商店中。我一直听说我需要迁移数据 - 但我找不到任何关于如何正确执行此操作的示例。有人知道怎么做吗?

我不断有人指出使用 migratePersistentStore:toURL:options:withType:error:,但我不知道如何使用它...

【问题讨论】:

    标签: ios core-data ios7 icloud


    【解决方案1】:

    这是一个带有 iCloud 控制面板的示例应用程序,用于将商店移入或移出 iCloud。要移动现有商店,您需要使用现有选项打开它,但请确保您使用 iOS7 选项作为目标商店。查看OSCDStackManager 中的示例应用程序代码,如果您有具体问题,请发布它们。 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;
    }
    

    【讨论】:

      【解决方案2】:

      您将现有商店移动到不同的路径,然后调用 migrate 方法并将 toURL 设置为您希望商店结束的路径。

      您需要传递其中包括 iCloud 商店需要设置的普遍设置的选项。

      迁移完成后,您应该有两个商店副本:您移到一边的非 iCloud 副本,以及设置了 iCloud 选项的新副本。如果您愿意,您现在可以删除旧存储,只需设置您的 Core Data 堆栈以使用 iCloud 存储。

      看看这个example中的一些方法。特别是,看看以“迁移”开头的那些。您应该能够弄清楚将数据迁移到新的云存储需要采取哪些步骤。

      Core Data 同步很难做到正确,尤其是当您开始迁移数据时。值得关注其他核心数据同步选项,例如 Wasabi SyncEnsembles。他们自动处理数据的迁移和合并。 (披露:我开发 Ensembles)

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-01-29
        • 1970-01-01
        • 2023-04-06
        • 1970-01-01
        • 1970-01-01
        • 2013-11-26
        • 2021-08-11
        • 1970-01-01
        相关资源
        最近更新 更多