美文网首页
NSGenericException "Start d

NSGenericException "Start d

作者: 五鲜谱 | 来源:发表于2017-06-05 16:44 被阅读258次

    链接: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...)

    相关文章

      网友评论

          本文标题:NSGenericException "Start d

          本文链接:https://www.haomeiwen.com/subject/jszifxtx.html