我有两个需要在应用程序启动时执行的代码路径:1。当Crashlytics检测到上次运行的报告时2.当它是干净启动时,即没有检测到崩溃报告。
Crashlytics提供(并建议)此方法用于检测崩溃:
- (void) crashlyticsDidDetectReportForLastExecution:(CLSReport *)report
但是文档明确指出在初始化期间不会同步调用该方法。因此,虽然我可以使用它来检测案例#1,但我认为不可能使用相同的方法来检测案例#2而不会引入竞争条件。
据我所知,当前框架没有公开任何方法来检查报告是否存在,无论是在Crashlytics.h还是CLSReport.h中。如果确实如此,我可以在框架初始化之前检查是否存在崩溃报告。
建议?
Mike提出的解决方案(来自Fabric)
迈克 - 我习惯假设委托方法和回调不能假设同步发生,或者在同一个线程上发生。你似乎在说我可以/应该在这里做出这个假设,这样(psdeudocode)就可以了:
(在AppDelegate中)
- (void)crashlyticsDidDetectReportForLastExecution:(CLSReport *)report completionHandler:(void (^)(BOOL))completionHandler {
self.HadCrash = YES;
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
completionHandler(YES);
}];
}
(在AppDelegate didFinishLaunching)
crashlytics.delegate = self;
[crashlytics init]; // presumably if the delegate method IS going to be called, it will be called here.
if (!HadCrash)
{ // do "no crash" stuff here }
来自Fabric的Mike,有两种方法可以用来了解发生的崩溃。
1)- (void)crashlyticsDidDetectReportForLastExecution:(CLSReport *)report;
此方法具有以下限制:
最重要的好处是报告崩溃的能力不会受到任何影响。
2)- (void)crashlyticsDidDetectReportForLastExecution:(CLSReport *)report completionHandler:(void (^)(BOOL submit))completionHandler;
completionHandler
的情况下调用YES
,否则不会发送报告。如果传入NO
,则呼叫将完成,但不会发送任何报告。以下是文档中的示例实现:
- (void)crashlyticsDidDetectReportForLastExecution:(CLSReport *)report completionHandler:(void (^)(BOOL))completionHandler {
// Use this opportunity to take synchronous action on a crash. See Crashlytics.h for
// details and implications.
// Maybe consult NSUserDefaults or show a UI prompt.
// But, make ABSOLUTELY SURE you invoke completionHandler, as the SDK
// will not submit the report until you do. You can do this from any
// thread, but that's optional. If you want, you can just call the
// completionHandler and return.
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
completionHandler(YES);
}];
}
我认为这解决了这个问题,但如果我错过了什么,请告诉我。