GCD取消异步块? [英] GCD cancel async block?

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

问题描述

我有用于在表格视图单元格中加载图片的常规代码

I have regular code for loading images in table view cells

    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{
        NSImage *image = file.image;
        dispatch_async(dispatch_get_main_queue(), ^{
            imageView.image = image;
        });
    });

问题是当我滚动太快我可以看到最后一个块在同一< c $ c> imageView 。这看起来真的很奇怪。有没有办法取消所有以前安排的操作一个 imageView (让我们说,他们都将有唯一的id)之前安排新的吗?

The problem is when I scroll too fast I can see that last block fires several times on same imageView. And that looks really weird. Is there any way to cancel all previously scheduled operations for one imageView (lets say they all will have unique id) before scheduling new one?

我的意思是,我想确保只有最后一次调度操作应该被执行,并且所有以前调度的都应该被删除。是否可以通过大中央调度?或者,我必须添加我自己的原子标志到imageView对象,并检查是否设置标志之前调用 imageView.image = image;

I mean, I want to be sure that only last scheduled operation should be executed and all all previously scheduled should be dropped. Is that possible by means of Grand Central Dispatch? Or I have to add my own atomic flags to imageView objects and check whether flags are set before calling imageView.image = image;

推荐答案

要使用GCD创建它,你必须使用自己的原子标志。

To archieve it using GCD you have to use your own atomic flag.

您已经取消了任务的开箱即可。它是一个NSOperationQueue。

But there is a better solution where you have cancellation of tasks out of the box. It is a NSOperationQueue.

您可以在此链接下阅读所需的一切: http://www.raywenderlich.com/19788/how-to-use-nsoperations-and-nsoperationqueues

You can read everything you need under this link: http://www.raywenderlich.com/19788/how-to-use-nsoperations-and-nsoperationqueues

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

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