dispatch_async(dispatch_get_main_queue(),^ {...});等到完成? [英] Does dispatch_async(dispatch_get_main_queue(), ^{...}); wait until done?

查看:442
本文介绍了dispatch_async(dispatch_get_main_queue(),^ {...});等到完成?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序中有一个场景,我想在一个方法中做一些耗费时间的任务,其中包括一些数据处理和UI更新。我的方法看起来像这样,

I have a scenario in my app, where I want to do some time consuming task which consists of some data processing as well as UI update, in a method. My method looks like this,

- (void)doCalculationsAndUpdateUIs {

    // DATA PROCESSING 1
    // UI UPDATE 1

    // DATA PROCESSING 2
    // UI UPDATE 2

    // DATA PROCESSING 3
    // UI UPDATE 3
} 

因为耗时我想在上面进行数据处理后台线程,使用,

As it is time consuming I wanted to do the data processing on the background thread, using,

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, NULL), ^{

但由于数据处理和UI更新都采用相同的方法,因此我想在主线程中仅移动UI更新,

But as both data processing and UI updates are in the same method, I wanted to move only the UI updates in main thread using,

dispatch_async(dispatch_get_main_queue(), ^{

最后我的方法看起来像这样,

Finally my method looks like this,

- (void)doCalculationsAndUpdateUIs {

    // DATA PROCESSING 1 
    dispatch_async(dispatch_get_main_queue(), ^{
        // UI UPDATE 1
    });

    /* I expect the control to come here after UI UPDATE 1 */

    // DATA PROCESSING 2
    dispatch_async(dispatch_get_main_queue(), ^{
        // UI UPDATE 2
    });

    /* I expect the control to come here after UI UPDATE 2 */

    // DATA PROCESSING 3
    dispatch_async(dispatch_get_main_queue(), ^{
        // UI UPDATE 3
    });
}

这真的有效吗?这真的是一个好习惯吗?实现这一目标的最佳方法是什么?

Does this really work? Is this really a good practice? What is the best way to achieve this?

P.S。所有这三个操作都是相互关联的。

P.S. All these three operations are interrelated to each other.

编辑:很抱歉。我在上面的代码中遗漏了一行。我的实际代码如下所示。

Sorry guys. I have missed a line in the above code. My actual code looks like this.

- (void)doCalculationsAndUpdateUIs {

    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{

        // DATA PROCESSING 1 
        dispatch_async(dispatch_get_main_queue(), ^{
            // UI UPDATE 1
        });

        /* I expect the control to come here after UI UPDATE 1 */

        // DATA PROCESSING 2
        dispatch_async(dispatch_get_main_queue(), ^{
            // UI UPDATE 2
        });

        /* I expect the control to come here after UI UPDATE 2 */

        // DATA PROCESSING 3
        dispatch_async(dispatch_get_main_queue(), ^{
            // UI UPDATE 3
        });
    });
}

再一次,我真的为这种混乱道歉。

Once again, I really apologize for the confusion.

推荐答案

不,它没有等待,你在这个样本中做的方式不是好习惯。

No it doesn't wait and the way you are doing it in that sample is not good practice.

dispatch_async 始终是异步。只是您将所有UI块排入同一队列,因此不同的块将按顺序运行,但与您的数据处理代码并行。

dispatch_async is always asynchronous. It's just that you are enqueueing all the UI blocks to the same queue so the different blocks will run in sequence but parallel with your data processing code.

如果您希望更新等待,可以改用 dispatch_sync

If you want the update to wait you can use dispatch_sync instead.

// This will wait to finish
dispatch_sync(dispatch_get_main_queue(), ^{
    // Update the UI on the main thread.
});

另一种方法是嵌套排队。我不会推荐它用于多个级别。

Another approach would be to nest enqueueing the block. I wouldn't recommend it for multiple levels though.

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
    // Background work

    dispatch_async(dispatch_get_main_queue(), ^{
        // Update UI

        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
            // Background work

            dispatch_async(dispatch_get_main_queue(), ^{
                // Update UI
            });
        });
    });
});

如果您需要更新UI以等待,那么您应该使用同步版本。有一个后台线程等待主线程是完全可以的。 UI更新应该非常快。

If you need the UI updated to wait then you should use the synchronous versions. It's quite okay to have a background thread wait for the main thread. UI updates should be very quick.

这篇关于dispatch_async(dispatch_get_main_queue(),^ {...});等到完成?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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