performSelector:withObject:afterDelay:和dispatch_after之间有什么权衡 [英] What are the tradeoffs between performSelector:withObject:afterDelay: and dispatch_after

查看:83
本文介绍了performSelector:withObject:afterDelay:和dispatch_after之间有什么权衡的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到的唯一功能差异是我可以取消使用 performSelector:withObject:afterDelay:安排的消息。我不知道如何取消提交给 dispatch_after 的块。 (请告诉我是否有办法做到这一点,我不知道)。

The only functional difference I have encountered is that I can cancel the message scheduled with performSelector:withObject:afterDelay:. I don't know of a way to cancel a block submitted to dispatch_after. (Please let me know if there is a way to do this that I do not know about).

我想了解更多关于:


  • 功能权衡(一个接口可以实现什么,而另一个接口可以实现?)

  • 性能权衡(是一个实现更高效?在哪些情况下?)

  • 样式权衡(我是否更喜欢某个任务的某个界面以更好地遵循常见的样式或惯例?)

推荐答案

dispatch_after 是新 Grand Central Dispatch ,这是iOS的扩展,旨在改进多核硬件上的并发代码执行。

dispatch_after is part of the new Grand Central Dispatch, which is an extension to iOS aimed at improving concurrent code execution on multicore hardware.

但总的来说,我认为它们总体上满足了不同的要求。 GCD允许对代码的并发执行进行更精细的分级控制。您可以在队列中调度块,删除它们,暂停,恢复等。这是一个更广泛的主题,在这里一般要考虑。此外,GCD提供了更多的同步选项。

But overall, I think they address different requirements overall. GCD allows to a much finer graded control over concurrent execution of code. You can schedule blocks on a queue, remove them, suspend, resume, etc. It is a broader topic to be considered here in general. Also, GCD provides many more synchronization options.

就与 performSelector 的比较而言,我认为这是一个优势 dispatch_after 正确地具有调度块的可能性,而无需定义选择器。
请参阅此讨论

As far as the comparison with performSelector, I think that one advantage dispatch_after rightly has is the possibility of scheduling a block without the need to define a selector. See this discussion.

总而言之,我对GCD没有多少经验,但我会说,除了块调度之外,当你只需要延迟一些你的UI中的选择器执行,一般不需要并发,我会使用 performSelector

On in all, I haven't got much experience with GCD, but I would say that apart from the block scheduling thing, when you simply need to delay some selector execution in your UI, without much a requirement for concurrency in general, I would use performSelector.

如果你想一想, performSelector 给你一个非常差的并发性,因为它只是在最短的时间后调度你的选择器在运行循环上执行。另一方面, dispatch_after 为您提供一个原则上似乎在纳秒级别的控件(!!这是我从Apple文档获得的,但我从未使用过它而且我不认为在iPhone上你会得到它,可能在MacOS上。)

If you think about it, performSelector gives you a very poor concurrency, since it simply schedules your selector for execution on the run loop after a minimum amount of time. On the other hand, dispatch_after gives you a control which seems in principle at the level of nanoseconds (!! this is what I get from Apple docs, but I have never used it and I don't think that on an iPhone you would get that, possibly on MacOS).

编辑:关于取消调度一个块,我从来没有尝试过取消调度块从队列中,但有可能 dispatch_release 也允许你控制它。如果没有,您可以为要取消的块定义自定义队列并释放整个队列(在块开始执行之前),如果这对您有意义的话。

about unscheduling a block, I have never tried to unschedule a block from a queue, but there is a possibility that dispatch_release also allows you to control that. If it does not, you could define your custom queue for the block that you want to cancel and release the whole queue (before the block starts being executed), if that ever makes sense to you.

至于性能,我真的不知道 performSelector 在里面做什么,但是如果它调度了一个线程,那么Apple 陈述使用GCD调度块只需要15条指令,而创建一个线程需要花费数百条。

As to performance, I really don't know what performSelector does inside, but if it schedules a thread, then Apple states that scheduling a block with GCD only costs 15 instructions, while creating a thread costs several hundred of them.

performSelector 外,不要忘记你可以选择使用 NSOperationQueue他们说,,它基于GCD,并且有一些开销,但不是很大。 NSOperationQueue 当然可以取消。

Apart from performSelector, don't forget you have the option of using NSOperationQueue, which is based on GCD, and has some overhead overt it but not that big, they say. NSOperationQueue certainly offers the possibility of cancelling.

这篇关于performSelector:withObject:afterDelay:和dispatch_after之间有什么权衡的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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