DispatchQueue.sync与DispatchQueue.async之间的区别 [英] Difference Between DispatchQueue.sync vs DispatchQueue.async

查看:249
本文介绍了DispatchQueue.sync与DispatchQueue.async之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图了解Dispatch Sync和Dispatch Async,我知道它以GCD的同步和异步方式执行.但是当我尝试下面的代码时,它给了我奇怪的情况.

Im trying to understand the Dispatch Sync and Dispatch Async, I know that its executes in sync and async manner of GCD. But when i try the below code it gave me weird scenario.

我在Playground和Sync块中执行了3次测试以下代码,而async块给出了NSException.

I testing the below code in Playground and Sync block executed 3times and the async block gave the NSException.

//:基于UIKit的Playground,用于呈现用户界面

//: A UIKit based Playground for presenting user interface

import UIKit
import PlaygroundSupport

class MyViewController : UIViewController {
    override func loadView() {
        let view = UIView()
        view.backgroundColor = .white

        let que = DispatchQueue.init(label: "testing")
        // Executed 3 times
        que.sync {
            for i in 0...10 {
                print(i)
            }
        }
        // Giving me NSException
        que.async {
            let label = UILabel()
            label.frame = CGRect(x: 150, y: 200, width: 200, height: 20)
            label.text = "Hello World!"
            label.textColor = .black

            view.addSubview(label)
            self.view = view
            print("Label Added to Text View")
        }
    }
}
// Present the view controller in the Live View window
PlaygroundPage.current.liveView = MyViewController()

为什么要执行3次同步块.以及为什么会出现NSException错误.

Why it executes 3 times of sync block. and why there is an NSException error.

推荐答案

同步将停止当前线程,直到完成您分配给它的任务为止.

Sync will stop the current thread until it has finished the task you are assigning to it.

异步将继续当前线程,并在当前线程之后或之后执行任务.

Async will continue with the current thread and will execute the task in parallel or after the current thread.

为什么会有意外的行为?

Why it has unexpected behaviour?

这是因为loadView()希望在执行view属性后将其分配给view属性,这是通过异步操作完成的,它将在loadView完成后执行.

That is because loadView() expects to have a UIView assigned to the view property after it has been executed, which you are doing it with async, which will be executed after loadView finishes.

该异常可能是因为您没有按时分配UIView,或者是因为您正在私有队列中处理UI.用户界面应始终在主线程中处理.

The exception might be because you are not assigning a UIView on time or because you are handling the UI in your private Queue. UI should always be handled in the main thread.

您的变量que是一个专用队列,因为您未指定否则它指向后台线程.

Your variable que is a private queue, and because you didn't specify otherwise it is pointing to a background thread.

像这样编辑代码可能对您有帮助:

Editing your code like this might help you:

import UIKit
import PlaygroundSupport

class MyViewController : UIViewController {
    override func loadView() {
        let view = UIView()
        view.backgroundColor = .white

        let que = DispatchQueue.init(label: "testing")
        // Executed 3 times
        que.sync {
            for i in 0...10 {
                print(i)
            }
        }
        // Giving me NSException
        DispatchQueue.main.async {
            let label = UILabel()
            label.frame = CGRect(x: 150, y: 200, width: 200, height: 20)
            label.text = "Hello World!"
            label.textColor = .black

            view.addSubview(label)

            print("Label Added to Text View")
        }
        self.view = view
    }
}

// Present the view controller in the Live View window
PlaygroundPage.current.liveView = MyViewController()

这篇关于DispatchQueue.sync与DispatchQueue.async之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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