From ab86065f38e5960a8dd4da136de9dd9af33883cf Mon Sep 17 00:00:00 2001 From: zhongwuzw Date: Sun, 8 Jul 2018 14:26:38 +0800 Subject: [PATCH] Fix image prefetcher completion logical handler --- SDWebImage/SDWebImagePrefetcher.m | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/SDWebImage/SDWebImagePrefetcher.m b/SDWebImage/SDWebImagePrefetcher.m index c4490cea..035354eb 100644 --- a/SDWebImage/SDWebImagePrefetcher.m +++ b/SDWebImage/SDWebImagePrefetcher.m @@ -15,8 +15,9 @@ // These value are just used as incrementing counter, keep thread-safe using memory_order_relaxed for performance. atomic_ulong _skippedCount; atomic_ulong _finishedCount; - atomic_ulong _totalCount; atomic_flag _isAllFinished; + + unsigned long _totalCount; } @property (nonatomic, copy, readwrite) NSArray *urls; @@ -105,7 +106,7 @@ // Current operation finished [sself callProgressBlockForToken:token imageURL:imageURL]; - if (atomic_load_explicit(&(token->_finishedCount), memory_order_relaxed) + atomic_load_explicit(&(token->_skippedCount), memory_order_relaxed) >= atomic_load_explicit(&(token->_totalCount), memory_order_relaxed)) { + if (atomic_load_explicit(&(token->_finishedCount), memory_order_relaxed) == token->_totalCount) { // All finished if (!atomic_flag_test_and_set_explicit(&(token->_isAllFinished), memory_order_relaxed)) { [sself callCompletionBlockForToken:token]; @@ -138,7 +139,7 @@ NSUInteger tokenFinishedCount = [self tokenFinishedCount]; NSUInteger tokenTotalCount = [self tokenTotalCount]; NSUInteger finishedCount = atomic_load_explicit(&(token->_finishedCount), memory_order_relaxed); - NSUInteger totalCount = atomic_load_explicit(&(token->_totalCount), memory_order_relaxed); + NSUInteger totalCount = token->_totalCount; dispatch_async(self.delegateQueue, ^{ if (shouldCallDelegate) { [self.delegate imagePrefetcher:self didPrefetchURL:url finishedCount:tokenFinishedCount totalCount:tokenTotalCount]; @@ -173,7 +174,7 @@ NSUInteger tokenTotalCount = 0; @synchronized (self.runningTokens) { for (SDWebImagePrefetchToken *token in self.runningTokens) { - tokenTotalCount += atomic_load_explicit(&(token->_totalCount), memory_order_relaxed); + tokenTotalCount += token->_totalCount; } } return tokenTotalCount;