NSGenericException "Start date cannot be later in time than end date!" in -[NSURLSessionTaskMetrics _initWithTask:]

链接:http://www.openradar.me/28301343

I can easily repro this crash on iOS 10.0 and 10.1 by manually adjusting the time in the device (via Settings app) while an NSURLSessionDataTask is in progress. It seems to be already fixed on iOS 10.2 beta 3 though.
I've implemented a quick workaround using swizzling. It's not pretty but it seems to work, i.e. avoids the crash at the expense of not having task metrics available for the task(s) affected by the date/time change.

@interface NSURLSessionTask(YourCategoryName)
@property double startTime;
@end
@interface NSURLSessionTaskMetrics()
- (instancetype)_initWithTask:(NSURLSessionTask *)task;
@end
@interface NSURLSessionTaskMetrics(YourCategoryName)
- (instancetype)xxx_initWithTask:(NSURLSessionTask *)task;
@end
@implementation NSURLSessionTaskMetrics(YourCategoryName)
- (instancetype)xxx_initWithTask:(NSURLSessionTask *)task { 
    if ([NSDate timeIntervalSinceReferenceDate] - task.startTime < 0) {
        CFRelease((__bridge CFTypeRef)(self));
        return nil;
    } 
    return [self xxx_initWithTask:task];
}
@end
+load() {
    Method originalInit = class_getInstanceMethod([NSURLSessionTaskMetrics class], @selector(_initWithTask:));
    Method xxxInit = class_getInstanceMethod([NSURLSessionTaskMetrics class], @selector(xxx_initWithTask:));
    if (originalInit && xxxInit){ method_exchangeImplementations(originalInit, xxxInit);}
}

Not heavily tested, so use at your own risk.

By oscahie at Nov. 28, 2016, 5:26 p.m. (reply...)

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • **2014真题Directions:Read the following text. Choose the be...
    又是夜半惊坐起阅读 10,066评论 0 23
  • 效果图: 分析:2种思路 第一种思路:将九宫格也看成一组cell,即这个tableview拥有3组cell 第二种...
    小胖子2号阅读 1,111评论 0 0
  • 今夜 天幕突然哭了 就化成了 伤心的雨 刚开始 呜呜咽咽 抽抽泣泣 慢慢的 痛哭流涕 仰面暗语 再后来 嚎啕大哭 ...
    夜语婉婷阅读 289评论 0 0
  • 《三不朽》二十四年春,穆叔如晋。范宣子逆之,问焉,曰:“古人有言曰:‘死而不朽’,何谓也?”穆叔未对。宣子曰:“昔...
    游思集阅读 440评论 0 0
  • 前些日子摔了一跤,开始时以为见了淤青,就擦了些药。擦着药的日子,开始觉得越来越痛,妈妈说,那是起了药效。所以,相信...
    长亭微雨阅读 191评论 0 0