찾다

 >  Q&A  >  본문

ios - 关于sdwebimage的疑问

刚在看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?而且是不结束的时候取消,这是什么意思?

大家讲道理大家讲道理2893일 전341

모든 응답(3)나는 대답할 것이다

  • PHPz

    PHPz2017-04-18 09:07:56

    다운로드 결과(정상 완료 또는 오류 발생)가 발생하기 전에 코드는 항상 CFRunLoopRun() 또는 CFRunLoopRunInMode(kCFRunLoopDefaultMode, 10, false)에서 차단된다는 점을 이해할 수 있습니다. , start를 호출한 후 다운로드가 계속됩니다. 진행 중에는 다운로드가 완료되거나 오류가 발생할 때까지 이 차단이 해제되지 않습니다(두 경우 모두 CFRunLoopStop이 호출됩니다).

    이때, isFinished가 여전히 NO인 것으로 확인된다면, 타임아웃이 되었다는 의미이며, 현재 연결을 취소하고 타임아웃 오류가 발생합니다.

    회신하다
    0
  • 怪我咯

    怪我咯2017-04-18 09:07:56

    beginBackgroundTaskWithExpirationHandler: 백그라운드에 진입하여 시간 초과되면 도달하며 리소스 점유를 피하기 위해 연결을 취소해야 합니다.

    회신하다
    0
  • 巴扎黑

    巴扎黑2017-04-18 09:07:56

    핵심코드는 여기

    으아악

    CFRunLoopStop이 알 수 없음으로 호출될 때까지 현재 스레드를 무기한 차단합니다

    회신하다
    0
  • 취소회신하다