<bdo id='EvYwQ'></bdo><ul id='EvYwQ'></ul>

      <small id='EvYwQ'></small><noframes id='EvYwQ'>

      <legend id='EvYwQ'><style id='EvYwQ'><dir id='EvYwQ'><q id='EvYwQ'></q></dir></style></legend>
      <tfoot id='EvYwQ'></tfoot>

      1. <i id='EvYwQ'><tr id='EvYwQ'><dt id='EvYwQ'><q id='EvYwQ'><span id='EvYwQ'><b id='EvYwQ'><form id='EvYwQ'><ins id='EvYwQ'></ins><ul id='EvYwQ'></ul><sub id='EvYwQ'></sub></form><legend id='EvYwQ'></legend><bdo id='EvYwQ'><pre id='EvYwQ'><center id='EvYwQ'></center></pre></bdo></b><th id='EvYwQ'></th></span></q></dt></tr></i><div id='EvYwQ'><tfoot id='EvYwQ'></tfoot><dl id='EvYwQ'><fieldset id='EvYwQ'></fieldset></dl></div>

        performSelector:withObject:afterDelay: 和 dispatch_after 之间的权

        What are the tradeoffs between performSelector:withObject:afterDelay: and dispatch_after(performSelector:withObject:afterDelay: 和 dispatch_after 之间的权衡是什么)
          <i id='8PAVP'><tr id='8PAVP'><dt id='8PAVP'><q id='8PAVP'><span id='8PAVP'><b id='8PAVP'><form id='8PAVP'><ins id='8PAVP'></ins><ul id='8PAVP'></ul><sub id='8PAVP'></sub></form><legend id='8PAVP'></legend><bdo id='8PAVP'><pre id='8PAVP'><center id='8PAVP'></center></pre></bdo></b><th id='8PAVP'></th></span></q></dt></tr></i><div id='8PAVP'><tfoot id='8PAVP'></tfoot><dl id='8PAVP'><fieldset id='8PAVP'></fieldset></dl></div>
            <tbody id='8PAVP'></tbody>
          <tfoot id='8PAVP'></tfoot>

            <small id='8PAVP'></small><noframes id='8PAVP'>

                  <bdo id='8PAVP'></bdo><ul id='8PAVP'></ul>

                • <legend id='8PAVP'><style id='8PAVP'><dir id='8PAVP'><q id='8PAVP'></q></dir></style></legend>

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

                  问题描述

                  我遇到的唯一功能差异是我可以取消使用 performSelector:withObject:afterDelay: 安排的消息.我不知道有什么方法可以取消提交给 dispatch_after 的块.(如果有我不知道的方法,请告诉我).

                  我想了解更多:

                  • 功能权衡(用一个接口而不是另一个接口可以实现什么?)
                  • 性能权衡(一种实现更有效吗?在哪些情况下?)
                  • 风格上的权衡(我是否应该为某些任务选择一个界面以更好地遵循常见的风格或约定?)

                  解决方案

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

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

                  就与 performSelector 的比较而言,我认为 dispatch_after 的一个优点是可以调度块而不需要定义选择器.请参阅此讨论.p>

                  总的来说,我对 GCD 没有太多经验,但我想说的是,除了块调度之外,当您只需要延迟 UI 中的某些选择器执行时,通常对并发性没有太多要求,我会使用 performSelector.

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

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

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

                  除了performSelector,别忘了你可以选择使用NSOperationQueue,它是基于GCD的,有一些开销,但不是很大,他们说.NSOperationQueue 当然提供了取消的可能性.

                  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).

                  I'd like to know more about:

                  • functional tradeoffs (What else can be accomplished with one interface but not the other?)
                  • performance tradeoffs (Is one implementation more efficient? In which cases?)
                  • stylistic tradeoffs (Should I prefer one interface for certain tasks to better follow common styles or conventions?)

                  解决方案

                  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.

                  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.

                  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.

                  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.

                  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).

                  EDIT: 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.

                  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.

                  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 之间的权衡是什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  What values should I use for iOS boolean states?(我应该为 iOS 布尔状态使用什么值?)
                  Why does Objective-C use YES/NO macro convention instead of true/false?(为什么 Objective-C 使用 YES/NO 宏约定而不是 true/false?)
                  How do I get NSJSONSerialization to output a boolean as true or false?(如何让 NSJSONSerialization 将布尔值输出为真或假?)
                  Is there any difference between bool, Boolean, and BOOL in Objective-C?(Objective-C 中的 bool、Boolean 和 BOOL 之间有什么区别吗?)
                  BOOL to NSString(布尔到 NSString)
                  Set bool property of all objects in the array(设置数组中所有对象的布尔属性)
                • <legend id='5oAJL'><style id='5oAJL'><dir id='5oAJL'><q id='5oAJL'></q></dir></style></legend>
                    • <bdo id='5oAJL'></bdo><ul id='5oAJL'></ul>

                        <i id='5oAJL'><tr id='5oAJL'><dt id='5oAJL'><q id='5oAJL'><span id='5oAJL'><b id='5oAJL'><form id='5oAJL'><ins id='5oAJL'></ins><ul id='5oAJL'></ul><sub id='5oAJL'></sub></form><legend id='5oAJL'></legend><bdo id='5oAJL'><pre id='5oAJL'><center id='5oAJL'></center></pre></bdo></b><th id='5oAJL'></th></span></q></dt></tr></i><div id='5oAJL'><tfoot id='5oAJL'></tfoot><dl id='5oAJL'><fieldset id='5oAJL'></fieldset></dl></div>

                            <tbody id='5oAJL'></tbody>

                            <tfoot id='5oAJL'></tfoot>

                            <small id='5oAJL'></small><noframes id='5oAJL'>