将 iCloud 迁移到本地持久存储
Posted
技术标签:
【中文标题】将 iCloud 迁移到本地持久存储【英文标题】:Migrating iCloud to local persistent store 【发布时间】:2015-02-15 17:20:35 【问题描述】:我想不出如何将 iCloud 商店迁移到本地商店。 从一方面我得到消息崩溃:
The specified persistent store was not found
另一边
NSUnderlyingException = "Can't add the same store twice"
关键是没有明确的教程如何迁移商店(或者至少我找不到)。到目前为止,我发现的一切都是在网络上传播的信息片段,没有多大意义。
这是我的代码:
- (NSPersistentStoreCoordinator *) cloudPersistentStoreCoordinator
self.storeURL = [[MMNUtilities localDocumentsURL] URLByAppendingPathComponent:@"myapp.sqlite"];
NSPersistentStoreCoordinator *persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:self.managedObjectModel];
NSDictionary *storeOptions = @NSPersistentStoreUbiquitousContentNameKey: @"myapp";
NSError *error = nil;
self.persistentStore = [persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType
configuration:nil
URL:self.storeURL
options:storeOptions
error:&error];
if (error)
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
return persistentStoreCoordinator;
- (BOOL) didMigrateiCloudStoreToLocalStore
NSError *error = nil;
self.persistentStoreCoordinator = [self cloudPersistentStoreCoordinator];
NSDictionary *storeOptions = @NSPersistentStoreUbiquitousContentNameKey: @"myapp",
NSPersistentStoreRemoveUbiquitousMetadataOption : @YES;
NSPersistentStore *localStore = [self.persistentStoreCoordinator migratePersistentStore:self.persistentStore <<< Exception happens here
toURL:self.storeURL
options:storeOptions
withType:NSSQLiteStoreType
error:&error];
if (error)
NSLog(@"Error: %@", error.description);
[[NSNotificationCenter defaultCenter] postNotificationName:HnHCoreDataPersistentStoreMigrationFailed object:self];
return NO;
return [self reloadStore:localStore];
我在控制台中收到以下消息:
CoreData: error: -addPersistentStoreWithType:SQLite configuration:PF_DEFAULT_CONFIGURATION_NAME URL:file:///var/mobile/Containers/Data/Application/EFC750F6-D10E-4B8A-9F83-C77621218DB7/Documents/myapp.sqlite options:
NSPersistentStoreRemoveUbiquitousMetadataOption = 1;
NSPersistentStoreUbiquitousContentNameKey = myapp;
"_NSNotifyObserversOfStoreChange" = 0;
... returned error Error Domain=NSCocoaErrorDomain Code=134080 "The operation couldn’t be completed. (Cocoa error 134080.)" UserInfo=0x15deee80 NSUnderlyingException=Can't add the same store twice with userInfo dictionary
NSUnderlyingException = "Can't add the same store twice";
任何帮助将不胜感激。
【问题讨论】:
【参考方案1】:您需要创建一个新的本地商店使用不同的名称。
【讨论】:
你的意思是myapp.sqlite
应该有不同的 iCloud 和 Local 名称吗?他们存储在不同的位置也没关系?以上是关于将 iCloud 迁移到本地持久存储的主要内容,如果未能解决你的问题,请参考以下文章
将 PersistentStoreCoordinator 从本地迁移到 iCloud 时数据重复