ios - 关于sdwebimage的疑问
大家讲道理
大家讲道理 2017-04-18 09:05:56
0
3
329

刚在看sdwebimage的源码,在SDWebImageDownloaderOperation这个类的start方法里,

- (void)start {
    @synchronized (self) {
        if (self.isCancelled) {
            self.finished = YES;
            [self reset];
            return;
        }

#if TARGET_OS_IPHONE && __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_4_0
        if ([self shouldContinueWhenAppEntersBackground]) {
            __weak __typeof__ (self) wself = self;
            self.backgroundTaskId = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{
                __strong __typeof (wself) sself = wself;

                if (sself) {
                    [sself cancel];

                    [[UIApplication sharedApplication] endBackgroundTask:sself.backgroundTaskId];
                    sself.backgroundTaskId = UIBackgroundTaskInvalid;
                }
            }];
        }
#endif

        self.executing = YES;
        self.connection = [[NSURLConnection alloc] initWithRequest:self.request delegate:self startImmediately:NO];
        self.thread = [NSThread currentThread];
    }

    [self.connection start];

    if (self.connection) {
        if (self.progressBlock) {
            self.progressBlock(0, NSURLResponseUnknownLength);
        }
        dispatch_async(dispatch_get_main_queue(), ^{
            [[NSNotificationCenter defaultCenter] postNotificationName:SDWebImageDownloadStartNotification object:self];
        });

        if (floor(NSFoundationVersionNumber) <= NSFoundationVersionNumber_iOS_5_1) {
            // Make sure to run the runloop in our background thread so it can process downloaded data
            // Note: we use a timeout to work around an issue with NSURLConnection cancel under iOS 5
            //       not waking up the runloop, leading to dead threads (see https://github.com/rs/SDWebImage/issues/466)
            CFRunLoopRunInMode(kCFRunLoopDefaultMode, 10, false);
        }
        else {
            CFRunLoopRun();
        }

        if (!self.isFinished) {
            [self.connection cancel];
            [self connection:self.connection didFailWithError:[NSError errorWithDomain:NSURLErrorDomain code:NSURLErrorTimedOut userInfo:@{NSURLErrorFailingURLErrorKey : self.request.URL}]];
        }
    }
    else {
        if (self.completedBlock) {
            self.completedBlock(nil, nil, [NSError errorWithDomain:NSURLErrorDomain code:0 userInfo:@{NSLocalizedDescriptionKey : @"Connection can't be initialized"}], YES);
        }
    }

#if TARGET_OS_IPHONE && __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_4_0
    if (self.backgroundTaskId != UIBackgroundTaskInvalid) {
        [[UIApplication sharedApplication] endBackgroundTask:self.backgroundTaskId];
        self.backgroundTaskId = UIBackgroundTaskInvalid;
    }
#endif
}
 [self.connection start];

开启了connection, 然后判断connection是否存在,如果存在的话,里面有一段

 if (!self.isFinished) {
            [self.connection cancel];
            [self connection:self.connection didFailWithError:[NSError errorWithDomain:NSURLErrorDomain code:NSURLErrorTimedOut userInfo:@{NSURLErrorFailingURLErrorKey : self.request.URL}]];
        }

这一段我就不理解了,如果它没有结束,就取消connection。刚刚开启connection,现在就取消connection?而且是不结束的时候取消,这是什么意思?

大家讲道理
大家讲道理

光阴似箭催人老,日月如移越少年。

全部回覆(3)
PHPzhong

可以這麼理解,在下載有結果(正常完成,或發生錯誤)之前,程式碼一直會阻塞在CFRunLoopRun() 或是CFRunLoopRunInMode(kCFRunLoopDefaultMode, 10, false) 這裡,就是說調音start 之後,下載一直就在進行中,一直到下載完成了或出錯了(這兩種情況都會呼叫CFRunLoopStop ),這個阻塞才會解除。

這時候,如果發現 isFinished 還是 NO, 那就說明是超時了,取消當前的連接,拋出超時錯誤。

刘奇

beginBackgroundTaskWithExpirationHandler:是在你進入後台超時的時候才會走到,當它超時的時候你就該取消連接以免佔用資源

巴扎黑

核心程式碼在於這裡

CFRunLoopRun();

它會無限期阻塞目前執行緒 直到呼叫CFRunLoopStop未知

熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板