菜鸟教程小白 发表于 2022-12-13 14:01:19

ios - 执行选择器 :afterDelay versus NSTimer:ScheduleTimerWithTimeInterval


                                            <p><p>我有一个方法我想在延迟后执行(不重复)。</p>

<p>我可以使用 performSelector:afterDelay 或者我可以安排一个 NSTimer 并将选择器指定为它的参数。</p>

<p>如果最终结果相同(即我的方法将在指定的时间延迟后被调用),那么使用其中一个的优点/缺点是什么。
我用哪一个没关系?</p>

<p>(如果相关,我的方法将在前台和当应用程序在 beginBackgroundTaskWithEcpirationHandler 可用的 10 分钟窗口期间移至后台时被调用)。</p>

<p>TIA</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>来自苹果对 NSObject 类关于 performSelector: 方法的引用</p>

<blockquote>
<p>This method sets up a timer to perform the aSelector message on the
current thread’s run loop. The timer is configured to run in the
default mode (NSDefaultRunLoopMode). When the timer fires, the thread
attempts to dequeue the message from the run loop and perform the
selector. It succeeds if the run loop is running and in the default
mode; otherwise, the timer waits until the run loop is in the default
mode.</p>
</blockquote>

<p>所以如果你只想打一个电话,我想你可以自由使用 <code>performSelector:afterDelay:</code></p></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 执行选择器 :afterDelay versus NSTimer:ScheduleTimerWithTimeInterval,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/10901502/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/10901502/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 执行选择器 :afterDelay versus NSTimer:ScheduleTimerWithTimeInterval