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

刚在看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 のままであることが判明した場合は、タイムアウトしたことを意味し、現在の接続をキャンセルし、タイムアウト エラーをスローします。

いいねを押す +0
刘奇

beginBackgroundTaskWithExpirationHandler: バックグラウンドに入ってタイムアウトになると到達します。タイムアウトになった場合は、リソースの占有を避けるために接続をキャンセルする必要があります。

いいねを押す +0
巴扎黑

コアコードはここにあります

リーリー

CFRunLoopStop が不明と呼ばれるまで、現在のスレッドを無期限にブロックします

いいねを押す +0
人気のチュートリアル
詳細>
最新のダウンロード
詳細>
ウェブエフェクト
公式サイト
サイト素材
フロントエンドテンプレート