当我尝试将新创建的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!
}
}];
答案 0 :(得分:3)
遗憾的是,这是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
}
虽然这是一种黑客行为,但它并没有帮助您实际保存文档。此外,无法保证文档稍后可以保存。但它可以避免崩溃。
通常,Core Data和iCloud不是最可靠的组合。我问过iOS版本,因为iOS 6.x比iOS 5差。因为你已经在6岁了,我不能建议转到6,希望能有更好的行为。