在iOS 7上工作时,我必须使用具有多个实体的核心数据调试应用程序。首先,我要求添加iCloud(而不是云套件)来保存所有数据。但之后,客户意识到他只想将一些实体,但不是全部都存入云中。将核心数据的一部分保存到云中
是否有可能?我需要使用几个NSPersistentStoreCoordinator吗? (应用程序已经使用了几个NSManagedObjectContext,每个实体一个)。或者,也许我可以做一些事情,当我收到通知:
NSPersistentStoreDidImportUbiquitousContentChangesNotification
并手动执行合并,但我真的不知道如何。
感谢您的帮助。
感谢汤姆·哈灵顿,我创建了两种配置:CloudConfiguration和LocalConfiguration和我添加在每个一些实体(这link助我也)。
然后,我添加在协调持久性存储:
// Configure persistentStoreCoordinator
NSError* error1 = nil;
NSString *cloudConfiguration = @"CloudConfiguration";
NSPersistentStore *store1 = [_persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType
configuration:@"CloudConfiguration"
URL:[self storeURLForConfiguration:cloudConfiguration]
options:@{ NSPersistentStoreUbiquitousContentNameKey : @"iCloudStore" }
error:&error1];
if (error1) {
NSLog(@"Error: %@ \n Description : %@ \nUser info : %@", error1, error1.description, error1.userInfo);
}
NSLog(@"*************** cloud store url ************** : %@", store1.URL);
NSError* error2 = nil;
NSString *localConfiguration = @"LocalConfiguration";
NSPersistentStore *localStore = [_persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType
configuration:localConfiguration
URL:[self storeURLForConfiguration:localConfiguration]
options:nil
error:&error2];
if (error2) {
NSLog(@"Error: %@ \n Description : %@ \nUser info : %@", error2, error2.description, error2.userInfo);
}
NSLog(@"*************** local store url ************** : %@", localStore.URL);
我所有的实体都指向一个商店只(云或本地)。不同商店的实体没有任何关系,我有一个模型。
所以我在这里,我第一次启动我的应用程序,似乎都配置得很好。但是,当我尝试在其他设备上或在同一设备上删除应用程序后,我有一个崩溃,刚开之后:使用本地存储:0
这里是崩溃日志:
[_PFUbiquityRecordImportOperation main](734): CoreData: Ubiquity: Error importing transaction log: <PFUbiquityTransactionLog: 0x16ed7f50>
transactionLogLocation: <PFUbiquityLocation: 0x16ed7ee0>: /var/mobile/Library/Mobile Documents/6ULEJ9RYTQ~fr~company~iCloudTestApp/CoreData/iCloudStore/mobile~E722813A-96E8-4E11-8DDE-56FF3837DEBD/iCloudStore/EU31J4aJIvvEyVMcWWYs1qgVajMk4_4fQxw1oe_Q0i0=/4C6B58B3-6C8D-4393-9B1E-8E48C7352091.1.cdt
transactionNumber: 1, exception: Invalid parameter value (bad entity)
User Info: (null)
2014-12-02 12:35:34.837 iCloudTestApp[1421:3b0b] -[_PFUbiquityRecordsImporter discoverAndImportAllAvailableLogs:error:](727): CoreData: Ubiquity: Exception while scanning for logs to import: Invalid parameter value (bad entity)
userInfo: (null)
它听起来很奇怪,因为它发生在合并之前。当然,在使用这两种配置进行测试之前,我会删除云中的所有数据。如果您有任何想法...
感谢您的帮助,我现在正在尝试:我不希望存储在不同商店文件中的实体之间存在任何关系。我刚刚创建了2个配置。现在我需要测试,我会更新我的问题以向我提供反馈。 – 2014-12-02 10:29:31
更新........ – 2014-12-02 19:04:23
感谢您的更新。所以也许我错过了一些东西,因为正如我在我编辑的文章中所说的那样,我在测试新配置之前删除了iCloud数据:设置 - > iCloud->存储和备份 - >管理存储(我的应用程序仍在开发中)。这就是为什么第一次发布不会让我觉得崩溃。 – 2014-12-03 10:07:47