【问题标题】:UIManagedDocument: Error/crash on creating new files when network is down (saving)UIManagedDocument:网络关闭时创建新文件时出错/崩溃(保存)
【发布时间】:2013-02-16 06:20:31
【问题描述】:

当我尝试将新创建的 UIManagedDocument 保存到 iCloud 并且网络关闭(例如飞行模式)时,我收到以下错误并崩溃(删除了十六进制代码和不可读的内容):

-[PFUbiquitySafeSaveFile waitForFileToUpload:](272): CoreData: Ubiquity:  <PFUbiquityPeerReceipt: ...>(0)
permanentLocation: <PFUbiquityLocation: ...>: /private/var/mobile/Library/Mobile Documents/XXXXXXXXXX~com~domain~AppName/TransactionLog/mobile.XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX/DocumentName/XXXXXXXXXXXXXXX/receipt.0.cdt
safeLocation: <PFUbiquityLocation: ...>: /private/var/mobile/Library/Mobile Documents/XXXXXXXXXX~com~domain~AppName/TransactionLog/mobile.XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX/DocumentName/XXXXXXXXXXXXXXX/mobile.XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX.0.cdt
currentLocation: <PFUbiquityLocation: ...>: /private/var/mobile/Library/Mobile Documents/XXXXXXXXXX~com~domain~AppName/TransactionLog/mobile.XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX/DocumentName/XXXXXXXXXXXXXXX/mobile.XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX.0.cdt

kv: (null)

Safe save failed for file, error: Error Domain=NSCocoaErrorDomain Code=512 "The file upload timed out." UserInfo=... {NSLocalizedDescription=The file upload timed out.}


*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'This NSPersistentStoreCoordinator has no persistent stores.  It cannot perform a save operation.'
*** First throw call stack:
(...)
libc++abi.dylib: terminate called throwing an exception

我不知道这个错误,但它表示无法保存文档,因为无法上传(当然,因为没有网络)。但我不明白为什么我无法使用保存完成处理程序捕获此错误:

[theDocumentToSave saveToURL:theDocumentToSave.fileURL
            forSaveOperation:UIDocumentSaveForCreating
           completionHandler:^(BOOL success) {
                 if (success) {
                     // Do somethings, go on, ...
                 } else {
                     // Catch error HERE, but this is never called!
                 }
}];

【问题讨论】:

  • 运行当前 iOS 6.1.2 的 iPhone 4 设备发生错误

标签: iphone ios core-data icloud uimanageddocument


【解决方案1】:

不幸的是,这代表了 Core Data 的 iCloud 集成中的一个内部错误。 UIManagedDocument 仍在尝试添加其数据存储,或者只是未能这样做,因为没有网络连接。这不是它应该如何工作的,但通常会出现故障或长时间延迟让 iCloud 启动并与 Core Data 一起运行。最坏的情况应该是——正如你所期望的——你的完成块将被调用,success 设置为NO。在这种情况下使您的应用崩溃不是您的错,但这也意味着您可能很难对此采取任何措施。

也许可以通过以下方式预测这次崩溃:

NSArray *persistentStores = theDocumentToSave.managedObjectContext.persistentStoreCoordinator.persistentStores;
if ([persistentStores count] == 0) {
    // exception is likely, should be at least 1
} else {
    // exception probably won't happen
}

不过,这有点像 hack,它并不能帮助您实际保存文档。另外,不能保证文档在以后的某个时间可以保存。不过,它可以避免崩溃。

一般来说,Core Data 加 iCloud 并不是最可靠的组合。我询问了 iOS 版本,因为 iOS 6.x 比 iOS 5 差。由于您已经在 6 上,我不能建议移到 6 以希望更好的行为。

【讨论】:

  • 您对 AppDelegate.m 的看法是正确的......显然,如果您使用 UIManagedDocument 执行此操作,则 AppDelegate.m 中不需要它,仅当您将其设置为自动时。与核心数据。另外,请查看链接下的 persistentStoreOptions 属性:developer.apple.com/library/ios/#DOCUMENTATION/UIKit/Reference/…
  • @Tom:非常感谢!您的代码似乎发现了错误。我将花一些时间在多个设备/iOS 版本上进行测试。是的,这是一种 hack,真可惜……Core Data/UIManagedDocument with iCloud 似乎有问题……
  • @Tom:啊,抱歉,下一个问题:此代码通常会阻止文档的创建。每次创建时persistentStores 的计数似乎都是 0(即使飞行模式已关闭并且存在网络连接)...
  • @FrankZp,很遗憾听到这个消息。由于 UIManagedDocument 的内部没有记录,很难知道什么会起作用。您可以做的最好的事情可能是检查网络可达性(请参阅 Apple 的可达性演示应用程序)以提高几率。
  • @TomHarrington:是的,我认为这是确定错误的唯一方法。检查一般互联网可访问性 ([Reachability reachabilityForInternetConnection]) 是否足够,或者您是否建议额外检查特定的 iCloud 主机(例如 [Reachability reachabilityWithHostName: @"www.icloud.com"])?
猜你喜欢
  • 2013-03-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-09-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多