【发布时间】:2013-12-21 01:29:48
【问题描述】:
我的目标是 iOS7,我使用 iCloud 和 UIManagedDocument。
我想将UIManagedDocument 从启用的 iCloud 迁移到仅限本地。迁移文档时,我不希望在云中出现任何与它相关的无处不在的内容,我希望它是 100% 本地的。
相反的情况,在启用 iCloud 的 UIManagedDocument 中转换仅本地 UIManagedDocument 很容易,只需在选项字典中添加两个选项 NSPersistentStoreUbiquitousContentNameKey 和 NSPersistentStoreUbiquitousContentURLKey 并迁移持久存储使用migratePersistentStore:toURL:options:withType:error:。
我认为从 iCloud 迁移回 Local Only 迁移将 nil 作为选项字典传递的 persistentStore 就足够了,但这不起作用:
NSPersistentStoreCoordinator *psc = self.managedDocument.managedObjectContext.persistentStoreCoordinator;
NSPersistentStore *ps = [psc.persistentStores objectAtIndex:0];
if(ps){
NSError *error;
[psc migratePersistentStore: ps
toURL: dbLocalOnlyURL
options: nil
withType: ps.type
error: &error];
if(error){
NSLog(@"Error migrating the DB to Local Only: %@", error);
}else{
NSLog(@"DB Migrated successfully to Local Only");
}
}
我收到 Cocoa 错误 256:操作无法完成。 NSUnderlyingException=无法打开数据库文件,NSSQLiteErrorDomain=14。
当我尝试执行操作时,使用本地存储仍然为 1。但我也尝试使用本地存储:0。
我认为这可能是因为我试图使用 documentState=UIDocumentStateNormal 迁移持久存储,所以我修改了如下代码:
[self.managedDocument closeWithCompletionHandler: ^(BOOL success){
if(success){
NSLog(@"Document closed succesfully. Performing the migration.");
NSPersistentStoreCoordinator *psc = self.managedDocument.managedObjectContext.persistentStoreCoordinator;
NSPersistentStore *ps = [psc.persistentStores objectAtIndex:0];
if(ps){
NSError *error;
[psc migratePersistentStore: ps
toURL: dbLocalOnlyURL
options: nil
withType: ps.type
error: &error];
if(error){
NSLog(@"Error migrating the DB to Local Only: %@", error);
}else{
NSLog(@"DB Migrated successfully to Local Only");
}
}
}else{
NSLog(@"Error closing the document.");
}
}];
但我仍然收到错误消息。有什么提示吗?
在迁移之前关闭文档是否总是更好的选择?
注意 1:如果我将持久存储迁移到 another url,则迁移不会出错,但我想要的是完全相同的 UIManagedDocument从 iCloud 迁移到仅限本地。
注意 2:我也在 Apple 开发者论坛上发布了这个问题。
【问题讨论】:
-
抱歉,周末离开了 - 请参阅下面的答案以获取详细说明的链接和演示应用程序工作的视频,并展示各种本地和无处不在的容器,同时将事物迁移到 iCloud 和从 iCloud 迁移.如果您有任何问题,请直接给我发电子邮件。
-
非常感谢朋友。猜猜你在我之前一直在同一条船上;)顺便说一句,我没有你的电子邮件,请写信给我,这样我就可以拥有它。
标签: ios objective-c core-data icloud uimanageddocument