将本地Core Data移至iCloud

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

我试过在我的持久存储选项中使用NSPersistentStoreUbiquitousContentNameKey 。 不幸的是,这个选项启用iCloud,但不会将任何本地数据传输到iCloud。 我似乎无法得到migratePersistentStore:toURL:options:withType:error:要么工作。 我提供了持久性存储,URL,iCloud选项等,它仍然不会将现有的本地数据迁移到iCloud。 以下是我如何使用该方法:

 - (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,而无需手动传输每个实体。

有任何想法吗? 我可以find很多关于 iCloud移回本地存储的文章,教程和文章 – 但是我想本地存储迁移 iCloud

这是你需要做的

  1. 创build一个本地的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 }]; }]; 

请注意,在执行此迁移之后,您需要使用新的URLconfiguration您在MOC中使用的持久性存储,并始终使用NSPersistentStoreUbiquitousContentNameKey键包含上面的iCloudOptions。

这是基于苹果的文档 。

完成后,您应该在标有CoreDataUbiquitySupport的模拟器文件夹(〜/ Library / Application Support / iPhone Simulator / …)的Documents文件夹中看到一个新的文件夹。 嵌套在那里是你的iCloud同步SQLite商店。

田田!


编辑:哦,并确保你已经创build了一个iCloud权利,并将其包含在您的包。 你应该可以在Xcode中做到这一点,但是你也可以在开发平台上更新它。

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

随时给我一个电子邮件进一步的帮助。 在这里不给你一个答案的道歉,但它可以是一个相当复杂的问题来处理。

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