2013-03-01 40 views
3

當我嘗試將新創建的UIManagedDocument保存到iCloud並且網絡出現故障(例如飛行模式)時,出現以下錯誤崩潰(hexcodes和不可讀的東西刪除):UIManagedDocument:網絡關閉時創建新文件時出現錯誤/崩潰(保存)

-[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

什麼版本的iOS? – 2013-03-01 18:52:12

+0

在運行當前iOS 6.1.2的iPhone 4設備上發生錯誤 – FrankZp 2013-03-01 18:53:31

回答

3

這不幸的是代表了核心數據的集成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 
} 

這是怎樣的一個黑客雖然,它不能幫助你居然保存文檔。另外,不能保證文檔在稍後的時間可以保存。但它可以避免崩潰。

一般來說,核心數據加iCloud並不是最可靠的組合。我詢問了iOS版本,因爲iOS 6.x比iOS 5更壞。既然你已經在6上了,但我不能建議爲了更好的行爲而移動到6。

+0

對於AppDelegate.m ......很明顯,如果您使用UIManagedDocument進行設置,您不需要AppDelegate.m,只有當你設置自動。與核心數據。另外,請查看鏈接下的persistentStoreOptions屬性:http://developer.apple.com/library/ios/#DOCUMENTATION/UIKit/Reference/UIManagedDocument_Class/Reference/Reference.html – user1066524 2013-03-01 19:23:51

+0

@Tom:非常感謝!你的代碼似乎趕上了錯誤。我需要一些時間在多個設備/ iOS版本上進行測試。是的,這是一種黑客,這是一種恥辱......與iCloud的核心數據/ UIManagedDocument似乎很buggy ... – FrankZp 2013-03-01 20:19:08

+0

@湯姆:唉,對不起,下一個問題:此代碼阻止一般文檔的創建。看起來創建時每次「persistentStores」的數量是0(即使飛機模式關閉並且存在網絡連接)...... – FrankZp 2013-03-01 20:32:27

相關問題