我iCloud
在我的应用程序。 我已删除iCloud
从我的应用程序,但在iOS 6上的应用程序崩溃,我得到这个消息:
-[NSPersistentStoreCoordinator addPersistentStoreWithType:configuration:URL:options:error:](1055):
CoreData: Ubiquity: Error:
先前已经加入到使用的iCloud集成方案协调持久性存储必须始终被添加到协调与存在于选择字典的选项。 如果您希望使用存储区而不iCloud
,迁移数据从iCloud
存储文件到本地存储的新存储文件。
我该如何解决这个问题? 我怎样才能从数据迁移iCloud
存储文件到本地存储的新存储文件?
是的,我有这个问题了。 我想转的iCloud存储到本地存储
方案1:移动managedObjects一个接一个的localStore。
但如果你有一个庞大的数据库,它会这么慢。
所以我昨天找到了第二个方案。
解决方法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];
文章来源: How can I migrate the data from the iCloud store file to a new store file in local storage?