将本地Core Data移至iCloud


哈哈哈哈
2025-03-14 05:42:23 (25天前)
  1. 如何启用iCloud


核心
</跨度>

数据
</跨度>
在已经使用本地存储的应用程序中

核心
</跨度>

数据
</跨度>

我试图在我的持久存储选项中使用NSPersistentStoreUbiquitousContentNameKey。不幸的是 iCloud存储。如果可能的话,我想搬到当地

核心
</跨度>

数据
</跨度>
到iCloud而无需手动转移每个实体。

有任何想法吗?我可以找到很多关于从iCloud移回本地存储的文章,教程和帖子 - 但我想从本地存储迁移到iCloud。

3 条回复
  1. 0# 無口君 | 2019-08-31 10-32



    这是你需要做的




    1. 创建本地NSPersistentStoreCoordinator


    2. 将现有持久性存储添加到该协调器,并存储对此新返回存储的引用。


    3. 调用方便的migratePersistStore:…提供来自#2的商店,文件目录中商店的URL,具有不同的文件名和所有重要选项,包括NSPersistentStoreUbiquitousContentNameKey键。

    4. </醇>


      这是代码,在线注释。




      1. NSURL *documentsDirectory = [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject];

      2. //This is the path to the new store. Note it has a different file name
        NSURL *storeURL = [documentsDirectory URLByAppendingPathComponent:@”TestRemote.sqlite”];

      3. //This is the path to the existing store
        NSURL *seedStoreURL = [documentsDirectory URLByAppendingPathComponent:@”Test.sqlite”];

      4. //You should create a new store here instead of using the one you presumably already have access to
        NSPersistentStoreCoordinator *coord = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:self.managedObjectModel];

      5. NSError seedStoreError;
        NSDictionary
        seedStoreOptions = @{ NSReadOnlyPersistentStoreOption: @YES };
        NSPersistentStore *seedStore = [coord addPersistentStoreWithType:NSSQLiteStoreType
        configuration:nil
        URL:seedStoreURL
        options:seedStoreOptions
        error:&seedStoreError];

      6. NSDictionary iCloudOptions = @{ NSPersistentStoreUbiquitousContentNameKey: @”MyiCloudStore };
        NSOperationQueue
        queue = [[NSOperationQueue alloc] init];

      7. //This is using an operation queue because this happens synchronously
        [queue addOperationWithBlock:^{
        NSError *blockError;
        [coord migratePersistentStore:seedStore
        toURL:storeURL
        options:iCloudOptions
        withType:NSSQLiteStoreType
        error:&blockError];

      8. NSOperationQueue *mainQueue = [NSOperationQueue mainQueue];
      9. [mainQueue addOperationWithBlock:^{
      10.     // This will be called when the migration is done
      11. }];
      12. }];

      13. </code>


      请注意,执行此迁移后,您需要使用新URL配置与MOC一起使用的持久性存储,并始终使用NSPersistentStoreUbiquitousContentNameKey键包含上面的iCloudOptions。



      这是基于
      <a href =“https://developer.apple.com/library/ios/documentation/DataManagement/Conceptual/UsingCoreDataWithiCloudPG/UsingSQLiteStoragewithiCloud/UsingSQLiteStoragewithiCloud.html#//apple_ref/doc/uid/TP40013491-CH3-SW3"rel =” noreferrer“>
      Apple的文档




      完成后,您应该在模拟器文件夹(〜/ Library / Application Support / iPhone Simulator / …)中的Documents文件夹中看到一个名为CoreDataUbiquitySupport的新文件夹。嵌套在你的iCloud同步sqlite商店深处。



      田田!





      编辑:哦,并确保您已创建一个iCloud权利,并将其包含在您的捆绑包中。您应该能够在Xcode中完成所有操作,但您也可以在开发门户上更新它。


  2. 1# 昵称不能为空 | 2019-08-31 10-32



    看看这个示例应用程序,其中包含将本地核心数据存储迁移到iCloud并再次返回的代码。最好阅读相关文档并在您的环境中构建示例应用程序以使它们正常工作,一旦它们正在工作,然后尝试重构您的代码以使用类似的方法。



    随时给我发电子邮件以获得进一步的帮助。抱歉没有给你答案,但处理起来可能是一个非常复杂的问题。




    http://ossh.com.au/design-and-technology/software-development/sample-library-style-ios-core-data-app-with-icloud-integration/


登录 后才能参与评论