问题
When I try to save a newly created UIManagedDocument to iCloud and the network is down (e.g. airplane mode) I get the following error(s) with a crash (hexcodes and unreadable stuff removed):
-[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
I don't know this error but it says that document couldn't be saved because no upload is possible (of course, because there is no network). But I can't understand why I can't catch this error with the save completion handler:
[theDocumentToSave saveToURL:theDocumentToSave.fileURL
forSaveOperation:UIDocumentSaveForCreating
completionHandler:^(BOOL success) {
if (success) {
// Do somethings, go on, ...
} else {
// Catch error HERE, but this is never called!
}
}];
回答1:
This unfortunately represents an internal bug in Core Data's iCloud integration. The UIManagedDocument
is still trying to get its data store added, or else has just failed to do so, because there's no network connection. That's not how it's supposed to work, but it's common to have failures or long delays getting iCloud up and running with Core Data. The worst case should be-- as you expect-- that your completion block would be called with success
set to NO
. Crashing your app in this scenario is not your fault, but that also means you may have a hard time doing anything about it.
You may be able to predict this crash by something like:
NSArray *persistentStores = theDocumentToSave.managedObjectContext.persistentStoreCoordinator.persistentStores;
if ([persistentStores count] == 0) {
// exception is likely, should be at least 1
} else {
// exception probably won't happen
}
That's kind of a hack though, and it doesn't help you actually save the document. Plus, there's no guarantee that the document will become save-able at some later time. It could avoid crashes, though.
In general, Core Data plus iCloud is not the most reliable of combinations. I asked about the iOS version because iOS 6.x is, let's say, less bad than iOS 5. Since you're already on 6 though, I can't suggest moving to 6 in the hope of better behavior.
来源:https://stackoverflow.com/questions/15162985/uimanageddocument-error-crash-on-creating-new-files-when-network-is-down-savin