Update SDWebImagePrefetcher weak-strong dance code into weakify && strongify

This commit is contained in:
DreamPiggy 2019-03-21 19:46:25 +08:00
parent 08c6e22906
commit 57ad6853ed
1 changed files with 31 additions and 32 deletions

View File

@ -107,39 +107,38 @@
- (void)startPrefetchWithToken:(SDWebImagePrefetchToken * _Nonnull)token { - (void)startPrefetchWithToken:(SDWebImagePrefetchToken * _Nonnull)token {
NSPointerArray *operations = token.loadOperations; NSPointerArray *operations = token.loadOperations;
for (NSURL *url in token.urls) { for (NSURL *url in token.urls) {
__weak typeof(self) wself = self; @weakify(self);
SDAsyncBlockOperation *prefetchOperation = [SDAsyncBlockOperation blockOperationWithBlock:^(SDAsyncBlockOperation * _Nonnull asyncOperation) { SDAsyncBlockOperation *prefetchOperation = [SDAsyncBlockOperation blockOperationWithBlock:^(SDAsyncBlockOperation * _Nonnull asyncOperation) {
__strong typeof(wself) strongSelf = wself; @strongify(self);
if (!strongSelf || asyncOperation.isCancelled) { if (!self || asyncOperation.isCancelled) {
return; return;
} }
id<SDWebImageOperation> operation = [strongSelf.manager loadImageWithURL:url options:strongSelf.options context:strongSelf.context progress:nil completed:^(UIImage * _Nullable image, NSData * _Nullable data, NSError * _Nullable error, SDImageCacheType cacheType, BOOL finished, NSURL * _Nullable imageURL) { id<SDWebImageOperation> operation = [self.manager loadImageWithURL:url options:self.options context:self.context progress:nil completed:^(UIImage * _Nullable image, NSData * _Nullable data, NSError * _Nullable error, SDImageCacheType cacheType, BOOL finished, NSURL * _Nullable imageURL) {
__strong typeof(wself) sself = wself; @strongify(self);
if (!sself) { if (!self) {
return; return;
} }
if (!finished) { if (!finished) {
return; return;
} }
atomic_fetch_add_explicit(&(token->_finishedCount), 1, memory_order_relaxed);
atomic_fetch_add_explicit(&(token->_finishedCount), 1, memory_order_relaxed); if (error) {
if (error) { // Add last failed
// Add last failed atomic_fetch_add_explicit(&(token->_skippedCount), 1, memory_order_relaxed);
atomic_fetch_add_explicit(&(token->_skippedCount), 1, memory_order_relaxed); }
}
// Current operation finished
// Current operation finished [self callProgressBlockForToken:token imageURL:imageURL];
[sself callProgressBlockForToken:token imageURL:imageURL];
if (atomic_load_explicit(&(token->_finishedCount), memory_order_relaxed) == token->_totalCount) {
if (atomic_load_explicit(&(token->_finishedCount), memory_order_relaxed) == token->_totalCount) { // All finished
// All finished if (!atomic_flag_test_and_set_explicit(&(token->_isAllFinished), memory_order_relaxed)) {
if (!atomic_flag_test_and_set_explicit(&(token->_isAllFinished), memory_order_relaxed)) { [self callCompletionBlockForToken:token];
[sself callCompletionBlockForToken:token]; [self removeRunningToken:token];
[sself removeRunningToken:token]; }
} }
} [asyncOperation complete];
[asyncOperation complete]; }];
}];
NSAssert(operation != nil, @"Operation should not be nil, [SDWebImageManager loadImageWithURL:options:context:progress:completed:] break prefetch logic"); NSAssert(operation != nil, @"Operation should not be nil, [SDWebImageManager loadImageWithURL:options:context:progress:completed:] break prefetch logic");
@synchronized (token) { @synchronized (token) {
[operations addPointer:(__bridge void *)operation]; [operations addPointer:(__bridge void *)operation];