繁体   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