Reputation: 3092
When I reading SDWebImage source code, I found a snippet
if (url)
{
__weak UIButton *wself = self;
id<SDWebImageOperation> operation = [SDWebImageManager.sharedManager downloadWithURL:url options:options progress:nil completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished)
{
__strong UIButton *sself = wself;
if (!sself) return;
if (image)
{
[sself setBackgroundImage:image forState:state];
}
if (completedBlock && finished)
{
completedBlock(image, error, cacheType);
}
}];
objc_setAssociatedObject(self, &operationKey, operation, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
It is easy to understand that for avoiding the retain cycle, it uses __weak keyword for the self, however, why it assigns wself to a strong variable sself, won't the block be still retain the self.
Upvotes: 0
Views: 151
Reputation: 6952
self
is an strong but it is out of the block scope, so the block will retain it. sself
is strong but it is in the block scope, it will be released at the end of the block, so no circle.
BTW the correct answer of this question is a good explanation about the block circle.
Upvotes: 1