簡體   English   中英

如何在iOS中捕獲iCloud異常?

[英]How to catch iCloud exceptions in iOS?

我的一個iOS應用程序將Core Data和iCloud一起使用了一段時間。 盡管這在大多數情況下都可以正常工作,但也存在問題,並且每個人都會崩潰。 問題:我無法通過捕獲異常來解決這些崩潰,因為我不知道在哪里這樣做。

導致應用程序崩潰的異常的最新示例是:

2014-02-07 10:23:45.118 MyApp[1410:3707] -[PFUbiquitySwitchboardEntryMetadata setUseLocalStorage:](754): CoreData: Ubiquity:  mobile~5508B01E-F77F-4AF1-88DE-2E2F8DC55932:MyAppCloudDB
Using local storage: 1

2014-02-07 10:23:45.892 MyApp[1410:3707] -[PFUbiquitySwitchboardEntryMetadata setUseLocalStorage:](754): CoreData: Ubiquity:  mobile~5508B01E-F77F-4AF1-88DE-2E2F8DC55932:MyAppCloudDB
Using local storage: 0

2014-02-07 10:24:28.632 MyApp[1410:1803] -[_PFUbiquityRecordImportOperation main](731): CoreData: Ubiquity:  Error importing transaction log: <PFUbiquityTransactionLog: 0x16177920>
transactionLogLocation: <PFUbiquityLocation: 0x160c3e80>: /var/mobile/Library/Mobile Documents/M889WYL2LY~com~example~MyApp/MyAppTransactionLogs/User~37B9682D-CED2-5F79-AF06-1FE99E7DEE9E/MyAppCloudDB/SZPp~RKudtVxEBMJzTVTupTv0WjG6i~o9Vj54dlknjc=/8AB09D17-5856-49B9-BE4C-A56451FB1EF4.1.cdt
transactionNumber: 34
, exception: *** setObjectForKey: key cannot be nil
User Info: (null)

2014-02-07 10:24:28.661 MyApp[1410:1803] -[_PFUbiquityRecordsImporter operation:failedWithError:](1003): CoreData: Ubiquity:  Import operation encountered an error: Error Domain=NSCocoaErrorDomain Code=134060 "The operation couldn’t be completed. (Cocoa error 134060.)" UserInfo=0x162a8b20 {exception=*** setObjectForKey: key cannot be nil}
userInfo: {
exception = "*** setObjectForKey: key cannot be nil";
}. While trying to import the log file at the URL: <PFUbiquityTransactionLog: 0x16177920>
transactionLogLocation: <PFUbiquityLocation: 0x160c3e80>: /var/mobile/Library/Mobile Documents/M889WYL2LY~com~example~MyApp/MyAppTransactionLogs/User~37B9682D-CED2-5F79-AF06-1FE99E7DEE9E/MyAppCloudDB/SZPp~RKudtVxEBMJzTVTupTv0WjG6i~o9Vj54dlknjc=/8AB09D17-5856-49B9-BE4C-A56451FB1EF4.1.cdt
transactionNumber: 34

2014-02-07 10:24:28.686 MyApp[1410:1803] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** setObjectForKey: key cannot be nil'
*** First throw call stack:

(0x30b47e83 0x3aea46c7 0x30a82ecb 0x309dd0cb 0x3099f061 0x309a1841 0x31476aa5 0x3151a96d 0x3b38de7b 0x3b38af93 0x3b38e745 0x3b38e9c5 0x3b4b8dff 0x3b4b8cc4)
libc++abi.dylib: terminating with uncaught exception of type NSException

事務日志似乎出了點問題。 當然,我想解決問題並保持應用程序運行,而不只是崩潰。 因此,我將必須能夠捕獲並處理異常。 但是我該怎么辦呢?

我什至不知道這種異常的來源。 我假設它是由UIManagedDocument或UIManagedObjectContext類拋出的,但我不確定。

即使我知道異常來自這些類之一,我仍然不知道如何捕獲它。 某些iCloud相關類在導入事務日志時在后台線程中引發異常。 我在代碼中所擁有的只是對UIManagedDocument實例的引用。 當然,我可以將對該實例的每個調用包裝在Try-Catch塊中,但是當我使用/調用該實例時,當對象自行完成某些工作時,不會引發異常。

甚至有可能捕獲此類異常嗎?

即使不可能捕獲並解決異常,也很高興知道已引發異常。 至少這將使我有機會在下次啟動該應用程序時處理該問題(向用戶顯示消息,與iCloud斷開連接等)。

那么,處理這些異常的最佳方法是什么?

非常感謝你!

我認為您不太可能找到捕獲異常的方法。 毫無疑問,它們在后台線程上沒有調用堆棧中的任何方法。 您可能會混淆其中一種Apple方法,但是它變得非常難看,而且無論如何在App Store中是不允許的。

不幸的是,Core Data + iCloud的不透明性使其得分不高。 您不知道它應該如何工作,並且當它確實失敗時,您真的無處可尋。

我建議提交雷達,但是您可能希望最好的是它已在iOS 8中修復。

值得考慮的替代同步框架(例如TICDSEnsembles )都可以與iCloud一起使用。 這樣做的好處是您至少擁有源代碼,可以查看出了什么問題,並經常在幾個小時內獲得修復。

(公開:我創建了Ensembles項目)

如果使用的是UIManagedDocument,則需要對其進行子類化並重寫一些方法以獲取錯誤。 特別是這兩個

- (id)contentsForType:(NSString *)typeName error:(NSError *__autoreleasing *)outError
{
    LOG(@"Auto-Saving Document");
    return [super contentsForType:typeName error:outError];
}

- (void)handleError:(NSError *)error userInteractionPermitted:(BOOL)userInteractionPermitted
{
    FLOG(@" error: %@", error.localizedDescription);
    NSArray* errors = [[error userInfo] objectForKey:NSDetailedErrorsKey];
    if(errors != nil && errors.count > 0) {
        for (NSError *error in errors) {
            FLOG(@" Error: %@", error.userInfo);
        }
    } else {
        FLOG(@" error.userInfo = %@", error.userInfo);
    }
}

不確定這些是否會引起您的特定錯誤。

此處提供有關核心數據和iCloud集成的更多信息。

http://ossh.com.au/design-and-technology/software-development/

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM