将本地Core数据移动到iCloud [英] Move local Core Data to iCloud

查看:153
本文介绍了将本地Core数据移动到iCloud的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在已使用本地存储Core Data的应用程序中启用iCloud Core Data?

How can I enable iCloud Core Data in an app which already uses local storage Core Data?

我试图使用 NSPersistentStoreUbiquitousContentNameKey 在我的永久存储选项。不幸的是,此选项启用iCloud,但不会将任何本地数据传输到iCloud。我似乎不能得到 migratePersistentStore:toURL:options:withType:error:工作。我提供持久存储,其URL,iCloud选项等,它仍然不会将现有本地数据迁移到iCloud。以下是我使用方法的方法:

I've tried to use NSPersistentStoreUbiquitousContentNameKey in my persistent store options. Unfortunately, this option enables iCloud but does not transfer any of the local data to iCloud. I can't seem to get migratePersistentStore:toURL:options:withType:error: to work either. I provide the persistent store, its URL, iCloud options, etc. and it still will not migrate the existing local data to iCloud. Here's how I'm using the method:

- (void)migratePersistentStoreWithOptions:(NSDictionary *)options {
    NSError *error;
    self.storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:[NSString stringWithFormat:@"%@.sqlite", self.SQLiteFileName]];

    NSPersistentStore *store = [self.persistentStoreCoordinator migratePersistentStore:self.persistentStoreCoordinator.persistentStores.firstObject toURL:self.storeURL options:options withType:NSSQLiteStoreType error:&error];
    if (store) NSLog(@"[CoreData Manager] Store was successfully migrated");
    else NSLog(@"[CoreData Manager] Error migrating persistent store: %@", error);
} 

本地存储与iCloud存储分离。如果可能,我想将本地核心数据移动到iCloud,而不用手动传输每个实体。

The local storage remains separate from the iCloud storage. If possible, I'd like to move the local Core Data to iCloud without manually transferring each entity.

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

Any ideas? I can find lots of articles, tutorials, and posts about moving back to local storage from iCloud - but I want to move from local storage to iCloud.

推荐答案

以下是您需要完成的操作

Here's what you'll need to do


  1. 创建本地NSPersistentStoreCoordinator

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

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

这是代码,注释在线。

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

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

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

//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];

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

NSDictionary *iCloudOptions = @{ NSPersistentStoreUbiquitousContentNameKey: @"MyiCloudStore" };
NSOperationQueue *queue = [[NSOperationQueue alloc] init];

//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];

    NSOperationQueue *mainQueue = [NSOperationQueue mainQueue];
    [mainQueue addOperationWithBlock:^{
        // This will be called when the migration is done
    }];
}];

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

Note that after you do this migration, you'll need to configure the persistent store you use with your MOC with the new URL and always include the iCloudOptions above with the NSPersistentStoreUbiquitousContentNameKey key.

这是基于 Apple的文档

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

After completion, you should see a new folder in your Documents folder in the simulator folder (~/Library/Application Support/iPhone Simulator/...) labeled CoreDataUbiquitySupport. Nested deep in there is your iCloud synced sqlite store.

Tada!

编辑:哦,并确保您创建了一个iCloud授权,并将其包含在您的捆绑包中。你应该能够在Xcode中做到这一点,但是你也可以在开发门户上更新它。

Oh and make sure you have created an iCloud entitlement and included it in your bundle. You should be able to do that all within Xcode, but you can update it on the development portal too.

这篇关于将本地Core数据移动到iCloud的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆