SDWebImage操作未取消 [英] SDWebImage operations not being canceled

查看:54
本文介绍了SDWebImage操作未取消的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个表格视图,其中包含几个供稿单元,每个供稿单元都有一些图像.我正在像这样加载图像:

I have a table view that includes several feed cells that each have a few images a piece. I'm loading in the images like this:

[timeLineCell.avatar setImageWithURL:[NSURL URLWithString:[feedAccount accountAvatarUrl]] placeholderImage:avatarPlaceholderImage options:SDWebImageRetryFailed];

这很好,但是在连接速度较慢的情况下,操作趋向于攀升,而不是删除相同图像的旧操作.也就是说,如果我在相同的单元格中上下滚动,则会在第二,第三,第四次等时间内将相同的图像添加到操作队列中.

This works fine, but on slow connections the operations tend to just climb instead of removing old operations for the same images. That is -- if I scroll down and back up through the same cells, it adds the same images into the operation queue for a second, third, fourth, etc time.

当单元在cellForRow中重新使用时,我还试图从下载队列中删除图像:

I'm also attempting to remove images from the download queue when the cell gets reused like this in cellForRow:

- (void)prepareForReuse {
    [super prepareForReuse];
    [self.avatar cancelCurrentImageLoad];
}

但是似乎该操作与SDWebImage方法中的操作队列中的任何内容都不匹配,因此它实际上并没有取消任何内容.如果我在共享管理器上运行cancelAll,它可以工作,但是显然不理想.

But it seems like the operation doesn't match anything in the operation queue in SDWebImage's methods, so it doesn't actually cancel anything. If I run cancelAll on the shared manager it works, but it's obviously not ideal.

我知道我只在该单元格上显示一张图像,但是我已经注释掉了除此图像加载以外的所有内容,问题仍然存在.如果我注释掉头像图片并允许下载另一张图片(类似加载),它也会持续存在.

I'm aware I'm only showing one image on this cell, but I have commented out everything except for this image loading and the problem persists. It also persists if I comment out the avatar image and allow a different image (loaded similarly) to download instead.

有人对此有任何提示吗?

Anyone have any tips on this?

P.S.我尝试将选项从SDWebImageRetryFailed更改为其他所有选项,包括完全没有选项,但这没什么区别.

P.S. I've tried changing the options from SDWebImageRetryFailed to other things including no options at all, but it made no difference.

P.P.S.我正在使用CocoaPods(3.4)上可用的最新版本的SDWebImage.

P.P.S. I'm using the latest version of SDWebImage available on CocoaPods (3.4).

推荐答案

为解决此问题,我实际上编辑了SDWebImage框架. 首先,我向SDWebImageManager添加了以下方法:

To solve this problem, I actually edited the SDWebImage framework a little bit. First, I added the following method to SDWebImageManager:

- (void)cancelOperation:(id<SDWebImageOperation>)operation {
    @synchronized(self.runningOperations)
    {
        [self.runningOperations removeObject:operation];
    }
}

然后,我在SDWebImageCombinedOperation上修改了- (void)cancel方法:

Then, I modified the - (void)cancel method on SDWebImageCombinedOperation:

- (void)cancel
{
    self.cancelled = YES;
    [[SDWebImageManager sharedManager] cancelOperation:self];
    if (self.cacheOperation)
    {
        [self.cacheOperation cancel];
        self.cacheOperation = nil;
    }
    if (self.cancelBlock)
    {
        self.cancelBlock();
        self.cancelBlock = nil;
    }
}

这并没有完全消除在队列中添加额外操作的问题,但是肯定会更快地清除现有的失败操作,因此问题不再是一个问题.我假设队列中似乎添加了更多操作,但这是因为现有操作尚未检查其isCancelled标志.

This didn't completely get rid of the problem of extra operations being added on the queue, but the existing failed ones definitely get cleared out much faster, and as such the problem winds up not being an issue anymore. I'm assuming it appears there are more operations being added in the queue, but that's because the existing ones haven't checked their isCancelled flags yet.

这篇关于SDWebImage操作未取消的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆