问题描述
我的应用中有iCloud.我从应用程序中删除了iCloud,但是在iOS 6应用程序中,我收到了此消息:
-[NSPersistentStoreCoordinator addPersistentStoreWithType:configuration:URL:options:error:](1055):
CoreData: Ubiquity: Error:必须始终将使用iCloud集成选项添加到协调器中,必须始终将其添加到协调器中,并在选项词典中添加选项.如果您想在没有iCloud的情况下使用商店,请将数据从iCloud存储文件迁移到本地存储中的新商店文件.
如何解决此错误?如何将数据从iCloud存储文件迁移到本地存储中的新商店文件?
推荐答案
是的,我也有这个问题. 我想将iCloud商店转到本地商店
解决方案1:将托管对象一一移动到本地店.
但是,如果您有一个大数据库,它将太慢.
所以我昨天找到了第二个解决方案.
解决方案2:编辑iCloud商店的元数据,
并将其保存到新位置.
删除" com.apple.coredata.ubiquity.*"元数据中的键, 您将获得一家本地商店.
这是我的解决方案代码2:
已经设置了一些属性:
@property (nonatomic, strong) NSPersistentStoreCoordinator *coordinator; @property (nonatomic, strong) NSManagedObjectContext *context; @property (nonatomic, strong) NSPersistentStore *iCloudStore; //represent the iCloud store already using //(after [coordinator addPersistentStore] you get this NSPersistentStore) @property (nonatomic, strong) NSURL *iCloudStoreURL; //represent the iCloud store real location //(it is the URL you send to the [coordinator addPersistentStore]) @property (nonatomic, strong) NSURL *iCloudStoreLocalVersionURL; //represent the location of local version store you want to save
和迁移方法:
-(void)migrateCloudStoreToLocalVersion { if(!self.iCloudStore) return; // remove previous local version [FILE_MANAGER removeItemAtURL:self.iCloudStoreLocalVersionURL error:nil]; // made a copy from original location to the new location [FILE_MANAGER copyItemAtURL:self.iCloudStoreURL toURL:self.iCloudStoreLocalVersionURL error:nil]; //prepare meta data NSDictionary *iCloudMetadata = [self.coordinator metadataForPersistentStore:self.iCloudStore].copy; NSMutableDictionary *localVersionMetadata = iCloudMetadata.mutableCopy; for(NSString * key in iCloudMetadata){ if([key hasPrefix:@"com.apple.coredata.ubiquity"]){ [localVersionMetadata removeObjectForKey:key]; } } //modify iCloud store [self.coordinator setMetadata:localVersionMetadata forPersistentStore:self.iCloudStore]; [self.coordinator setURL:self.iCloudStoreLocalVersionURL forPersistentStore:self.iCloudStore]; //save to the localVersion location [self.context save:nil]; //restore iCloud store [self.coordinator setMetadata:iCloudMetadata forPersistentStore:self.iCloudStore]; [self.coordinator setURL:self.iCloudStoreURL forPersistentStore:self.iCloudStore]; }
然后您可以使用iCloudStoreLocalVersionURL使用本地版本存储.
您可以将此本地版本存储用作本地商店,而不会出错.
注意:
注意元数据中的NSStoreUUIDKey,
您可以将其替换为新商店.
其他推荐答案
我相信您也必须更改UUID号码,在该应用程序的下一个运行中,我将两次加载同一商店的错误.所以我进行了此修改
if (!self.iCloudStore) return; NSError *error = nil; NSURL* localStoreURL = [self fallbackStoreURL]; NSFileManager *fm = [[NSFileManager alloc] init]; if (!self.fallbackStore) [self loadFallbackStore:&error]; NSString* fallBackUUID; //find UUID of original to put back in later NSDictionary *fallBackMetadata = [_psc metadataForPersistentStore:self.fallbackStore].copy; for(NSString* key in fallBackMetadata) { if([key hasPrefix:@"NSStoreUUID"]) { fallBackUUID = [fallBackMetadata objectForKey:key]; break; } } [fm removeItemAtURL:localStoreURL error:nil]; //prepare meta data NSDictionary *iCloudMetadata = [_psc metadataForPersistentStore:self.iCloudStore].copy; NSMutableDictionary *localVersionMetadata = iCloudMetadata.mutableCopy; for(NSString* key in iCloudMetadata) { if([key hasPrefix:@"com.apple.coredata.ubiquity"]) { [localVersionMetadata removeObjectForKey:key]; } if([key hasPrefix:@"NSStoreUUID"]) { if (fallBackUUID) [localVersionMetadata setObject:fallBackUUID forKey:key]; } } //modify iCloud store [_psc setMetadata:localVersionMetadata forPersistentStore:self.iCloudStore]; [_psc setURL:localStoreURL forPersistentStore:self.iCloudStore]; // make a copy from original location to the new location [fm copyItemAtURL:[self iCloudStoreURL] toURL:localStoreURL error:nil]; [_fallbackContext save:nil]; [_psc setMetadata:iCloudMetadata forPersistentStore:self.iCloudStore]; [_psc setURL:[self iCloudStoreURL] forPersistentStore:self.iCloudStore];