如何计划块在下一个运行循环迭代中运行? [英] How do you schedule a block to run on the next run loop iteration?

查看:205
本文介绍了如何计划块在下一个运行循环迭代中运行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在下一个运行循环迭代中执行。它不是那么重要,它是否在下一个运行循环的开始或结束执行,只是执行被推迟到当前运行循环中的所有代码已完成执行。

I want to be able to execute a block on the next run loop iteration. It's not so important whether it gets executed at the beginning or the end of the next run loop, just that execution is deferred until all code in the current run loop has finished executing.

我知道以下不工作,因为它与主运行循环交织,所以我的代码可能在下一个运行循环执行,但它可能不会。

I know the following doesn't work because it gets interleaved with the main run loop so my code might execute on the next run loop but it might not.

dispatch_async(dispatch_get_main_queue(),^{
    //my code
});

以下我相信遇到了与上述相同的问题:

The following I believe suffers the same problem as above:

dispatch_after(DISPATCH_TIME_NOW, dispatch_get_main_queue(), ^(void){
    //my code
});

现在我相信以下内容会放在最后

Now I believe the following would work as it is placed at the end of the current run loop (correct me if I'm wrong), would this actually work?

[self performSelector:@selector(myMethod) withObject:nil afterDelay:0];

使用 0 ?该文档说明:如果秒小于或等于0.0,则此方法将选择0.1毫秒的非负值。这是否保证在下一个运行循环迭代?

What about a timer with a 0 interval? The documentation states: If seconds is less than or equal to 0.0, this method chooses the nonnegative value of 0.1 milliseconds instead. Does this translate to guaranteeing execution on the next run loop iteration?

[NSTimer scheduledTimerWithTimeInterval:0 target:self selector:@selector(myMethod) userInfo:nil repeats:NO];

这是我可以想到的所有选项,但我还是没有更接近执行一个块

That's all the options I can think of but I'm still no closer to executing a block (as opposed to calling a method) on the next run loop iteration with the guarantee that it won't be any sooner.

推荐答案

您可以在下一个运行循环迭代中调用方法可能不知道运行循环在每次迭代中所做的一切。发生时, CFRunLoop 开源CoreFoundation包,所以我们可以看看它究竟是什么。运行循环看起来大致如下:

You might not be aware of everything that the run loop does in each iteration. (I wasn't before I researched this answer!) As it happens, CFRunLoop is part of the open-source CoreFoundation package, so we can take a look at exactly what it entails. The run loop looks roughly like this:

while (true) {
    Call kCFRunLoopBeforeTimers observer callbacks;
    Call kCFRunLoopBeforeSources observer callbacks;
    Perform blocks queued by CFRunLoopPerformBlock;
    Call the callback of each version 0 CFRunLoopSource that has been signalled;
    if (any version 0 source callbacks were called) {
        Perform blocks newly queued by CFRunLoopPerformBlock;
    }
    if (I didn't drain the main queue on the last iteration
        AND the main queue has any blocks waiting)
    {
        while (main queue has blocks) {
            perform the next block on the main queue
        }
    } else {
        Call kCFRunLoopBeforeWaiting observer callbacks;
        Wait for a CFRunLoopSource to be signalled
          OR for a timer to fire
          OR for a block to be added to the main queue;
        Call kCFRunLoopAfterWaiting observer callbacks;
        if (the event was a timer) {
            call CFRunLoopTimer callbacks for timers that should have fired by now
        } else if (event was a block arriving on the main queue) {
            while (main queue has blocks) {
                perform the next block on the main queue
            }
        } else {
            look up the version 1 CFRunLoopSource for the event
            if (I found a version 1 source) {
                call the source's callback
            }
        }
    }
    Perform blocks queued by CFRunLoopPerformBlock;
}

您可以看到有多种方法挂钩到运行循环。您可以创建要为任何所需的活动调用的 CFRunLoopObserver 。您可以创建版本0 CFRunLoopSource 并立即发出信号。您可以创建 CFMessagePorts 的连接对,将其中一个包装在版本1 CFRunLoopSource 中,然后向其发送消息。您可以创建 CFRunLoopTimer 。您可以使用 dispatch_get_main_queue CFRunLoopPerformBlock 对队列进行排队。

You can see that there are a variety of ways to hook into the run loop. You can create a CFRunLoopObserver to be called for any of the "activities" you want. You can create a version 0 CFRunLoopSource and signal it immediately. You can create a connected pair of CFMessagePorts, wrap one in a version 1 CFRunLoopSource, and send it a message. You can create a CFRunLoopTimer. You can queue blocks using either dispatch_get_main_queue or CFRunLoopPerformBlock.

您需要根据调度块的时间以及何时需要调用这些API来决定使用哪些API。

You will need to decide which of these APIs to use based on when you are scheduling the block, and when you need it to be called.

例如,在版本1源中处理,但是如果通过更新屏幕来处理触摸,那么在Core Animation事务提交之前实际上不执行更新,这发生在 kCFRunLoopBeforeWaiting observer。

For example, touches are handled in a version 1 source, but if you handle the touch by updating the screen, that update isn't actually performed until the Core Animation transaction is committed, which happens in a kCFRunLoopBeforeWaiting observer.

现在假设你想在处理触摸时调度块,但是你希望在事务提交之后执行。

Now suppose you want to schedule the block while you're handling the touch, but you want it to be executed after the transaction is committed.

您可以为 kCFRunLoopBeforeWaiting 活动添加您自己的 CFRunLoopObserver 此观察者可能会在Core Animation的观察者之前或之后运行,这取决于您指定的顺序和Core Animation指定的顺序。 (Core Animation目前指定的顺序为2000000,但是没有记录,所以它可以改变。)

You can add your own CFRunLoopObserver for the kCFRunLoopBeforeWaiting activity, but this observer might run before or after Core Animation's observer, depending on the order you specify and the order Core Animation specifies. (Core Animation currently specifies an order of 2000000, but that is not documented so it could change.)

为了确保你的块在Core Animation的观察者之后运行,观察者在 Core Animation的观察者之前运行,不要在观察者的回调中直接调用该块。相反,在该点使用 dispatch_async 将块添加到主队列。将块放在主队列上将迫使运行循环立即从其等待唤醒。它将运行任何 kCFRunLoopAfterWaiting 观察者,然后它将消耗主队列,这时它将运行你的块。

To make sure your block runs after Core Animation's observer, even if your observer runs before Core Animation's observer, don't call the block directly in your observer's callback. Instead, use dispatch_async at that point to add the block to the main queue. Putting the block on the main queue will force the run loop to wake up from its "wait" immediately. It will run any kCFRunLoopAfterWaiting observers, and then it will drain the main queue, at which time it will run your block.

这篇关于如何计划块在下一个运行循环迭代中运行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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