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...)

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • **2014真題Directions:Read the following text. Choose the be...
    又是夜半驚坐起閱讀 11,089評(píng)論 0 23
  • 效果圖: 分析:2種思路 第一種思路:將九宮格也看成一組cell,即這個(gè)tableview擁有3組cell 第二種...
    小胖子2號(hào)閱讀 1,175評(píng)論 0 0
  • 今夜 天幕突然哭了 就化成了 傷心的雨 剛開始 嗚嗚咽咽 抽抽泣泣 慢慢的 痛哭流涕 仰面暗語 再后來 嚎啕大哭 ...
    夜語婉婷閱讀 309評(píng)論 0 0
  • 《三不朽》二十四年春,穆叔如晉。范宣子逆之,問焉,曰:“古人有言曰:‘死而不朽’,何謂也?”穆叔未對(duì)。宣子曰:“昔...
    游思集閱讀 463評(píng)論 0 0
  • 前些日子摔了一跤,開始時(shí)以為見了淤青,就擦了些藥。擦著藥的日子,開始覺得越來越痛,媽媽說,那是起了藥效。所以,相信...
    長(zhǎng)亭微雨閱讀 210評(píng)論 0 0

友情鏈接更多精彩內(nèi)容