2014-02-07 147 views
1

我的一个iOS应用程序使用Core Data与iCloud一段时间。虽然这在大多数情况下都可以正常工作,但每个人都知道问题并崩溃。问题:我无法通过捕获异常来解决这些崩溃问题,因为我不知道在哪里执行此操作。如何在iOS中捕捉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断开连接等)。

那么,处理这些异常的最佳方法是什么?

非常感谢!

回答

0

我认为你不太可能找到一种方法来捕捉异常。他们毫无疑问在后台线程中,你的方法都不在调用堆栈中。你可以调整其中一种苹果方法,但它变得非常难看,无论如何也不允许在App Store中使用。

不幸的是,Core Data + iCloud的不透明度是对它的评分。你不知道它应该如何工作,当它确实如此失败时,你实在没有办法。

我建议提交雷达,但你也许可以期待的最好的是,它是固定在iOS的8

值得考虑替代的同步框架,如TICDSEnsembles,这与iCloud的两个工作。这样做的好处是您至少拥有源代码,可以看到发生了什么问题,并在几个小时内得到修复。

(披露:我创办了合奏项目)

0

如果您正在使用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); 
    } 
} 

不确定这些将拿起您的具体错误。

更多关于Core Data和iCloud的整合。

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