【发布时间】:2014-12-01 15:59:59
【问题描述】:
在 iOS 7 上工作时,我必须使用带有多个实体的 Core Data 来调试应用程序。首先,我要求添加 iCloud(而不是云套件)来保存所有数据。但随后,客户意识到他只想将一些实体而不是所有实体保存到云中。
有可能吗?我需要使用几个 NSPersistentStoreCoordinator 吗? (应用程序已经使用了几个 NSManagedObjectContext,每个实体一个)。或者当我收到通知时我可以做点什么:
NSPersistentStoreDidImportUbiquitousContentChangesNotification
并手动执行合并,但我真的不知道如何。
感谢您的帮助。
感谢 Tom Harrington,我创建了 2 个配置: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);
我的所有实体都只在一个商店中(云或本地)。不同商店中的实体没有任何关系,我只有一个模型。
所以我开始了,我第一次启动我的应用程序,一切似乎都配置得很好。但是,当我在删除应用程序后尝试在另一台设备上或同一台设备上时,我遇到了崩溃,就在得到:Using local storage: 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)
对我来说这听起来很奇怪,因为它发生在合并之前。当然,在使用这两种配置进行测试之前,我会删除云中的所有数据。如果你有任何想法......
【问题讨论】: