本文介绍了在SDWebImage代码中解释__weak和__strong使用原因的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我认为我很了解强弱关键词,但我不明白它是如何在下面的代码中使用的。此代码来自Olivier Poitrey的SDWebImage,可在github上找到。我理解这里描述的强弱关键词:

I think I understand strong and weak keywords well, but I don't understand how it's used in the code below. This code is from SDWebImage by Olivier Poitrey available on github. I understand strong and weak keywords as is described here: Explanation of strong and weak storage in iOS5

下面的代码以对我好奇的方式使用__weak和__strong关键字。这不是一个孩子 - 父母关系或委托模式,因为我习惯看到弱使用。但是,我确信这是一种经常使用的模式,正如我之前在其他代码中看到的那样。它在另一个线程上运行的块之前设置__weak引用。然后,在块内,它将弱引用设置为强引用。

The code below uses __weak and __strong keywords in a way that is curious to me. It is not a child-parent relationship or delegate pattern as I am used to seeing weak used. However, I'm sure that this is a pattern that is used often, as I've seen it before in other code. It sets a __weak reference before a block that runs on another thread. Then, within the block, it sets the weak reference to a strong reference.

我确信这个优雅而优雅的代码,所以我试图理解它。如果在块运行之前self不再存在,则弱自引用将为零。块运行时,强引用也将设置为零。因此,它将知道杀死其余的操作,因为自我不再存在。我做对了吗?

I am certain that this good and elegant code, so I'm trying to understand it. If "self" ceases to exist before the block runs, the weak self reference will zero out. When the block runs, the strong reference will be set to zero as well. Therefore, it will know to kill the rest of the operation since self doesn't exist anymore. Did I get this right?

现在,如果我们不使用__weak和__strong关键字会发生什么?如果我们只是在块内检查self == nil会怎么样? 自我永远不会是零,因为块复制整个树?

Now, what would happen if we didn't use __weak and __strong keywords? What if we just checked inside the block whether self == nil. Would "self" never be nil since the block copies the entire tree?

有人可以帮助揭开这段令人敬畏的代码的神秘面纱吗?有人可以验证或否定我的假设吗?

Can someone help demystify this awesome piece of code? Can someone verify or repudiate my hypotheses?

- (void)setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder options:(SDWebImageOptions)options progress:(SDWebImageDownloaderProgressBlock)progressBlock completed:(SDWebImageCompletedBlock)completedBlock;
{
    [self cancelCurrentImageLoad];

    self.image = placeholder;

    if (url)
    {
        __weak UIImageView *wself = self;
        id<SDWebImageOperation> operation = [SDWebImageManager.sharedManager downloadWithURL:url options:options progress:progressBlock completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished)
        {
            __strong UIImageView *sself = wself;
            if (!sself) return;
            if (image)
            {
                sself.image = image;
                [sself setNeedsLayout];
            }
            if (completedBlock && finished)
            {
                completedBlock(image, error, cacheType);
            }
        }];
        objc_setAssociatedObject(self, &operationKey, operation, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    }
}


推荐答案

downloadWithUrl:方法可能需要很长时间。在那段时间内,用户可能决定离开,无需使用 SDWebImage 对象。为了便于早期清理对象,外部 self 引用很弱。这样, downloadWithUrl 不会阻止 SDWebImage 被解除分配

The downloadWithUrl: method might take a long time. In that time, the user might decide to navigate away, eliminating the need for the SDWebImage object. To facilitate early cleanup of the object, the outer self reference is weak. This way, downloadWithUrl won't prevent the SDWebImage from being deallocated.

当然,如果你真的想使用 self ,你需要一个强大的参考。因此, downloadWithUrl 的完成后块会获得对 self 的强引用。如果对象在此时间消失, sself 将为 nil 。否则,它将是一个有效的强引用,表示 SDWebImage 对象仍在,对象将在此时完成其工作。

Of course, if you actually want to work with self, you need a strong reference. So, the on-completion block of downloadWithUrl grabs a strong reference to self. If the object goes away in this time, sself will be nil. Otherwise, it will be a valid strong reference, indicating that the SDWebImage object is still around, and the object will finish its work at this time.

这篇关于在SDWebImage代码中解释__weak和__strong使用原因的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-23 00:28