如何将数据从iCloud存储文件迁移到本地存储中的新存储文件?

我在我的应用程序中有iCloud 。 我已经从我的应用程序中删除了iCloud ,但在iOS 6应用程序崩溃,我得到这个消息:

  -[NSPersistentStoreCoordinator addPersistentStoreWithType:configuration:URL:options:error:](1055): 

CoreData: Ubiquity: Error:以前使用iCloud集成选项添加到协调器的持久性存储必须始终使用选项字典中的选项添加到协调器。 如果您希望在没有iCloud的情况下使用商店,请将数据从iCloud存储文件迁移到本地存储中的新存储文件。

我怎样才能解决这个错误? 如何将数据从iCloud存储文件迁移到本地存储中的新存储文件?

是的,我也有这个问题。 我想把一个iCloud商店变成一个本地商店


解决scheme1:将managedObjects一个接一个移动到localStore。

但是,如果你有一个大型的数据库,它会很慢。

所以我昨天发现了第二个解决scheme。


解决scheme2:编辑iCloud商店的元数据,

并保存到新的位置。

在元数据中删除“com.apple.coredata.ubiquity。*”键后,您将获得完全本地存储。


这是我的解决scheme2的代码:

有一些属性已经设置:

 @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

您可以select将其replace为新的商店。

我相信你也必须更换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];