iOS中的同步与异步队列 [英] Sync vs Async Queue in iOS

查看:82
本文介绍了iOS中的同步与异步队列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对JS有一些了解,并且正在通过iOS

I have some idea about JS and was going through iOS

我正在阅读此博客,并且对Swift中的异步和同步感到困惑(尽管我感觉在IOS和Javascript中是相同的),但是无论如何

I was reading this blog and am confused about async and sync in Swift (although I have a feeling that it's same in IOS and Javascript) but anyway

import UIKit 

func smile () {
    print('1')
}

func love() {
    print('2')
}

//Serial Queue

let queue = DispatchQueue(label: 'SerialQueue')

queue.async {
    for _ in 1...5 {
     smile()
  }
}

 queue.async {
    for _ in 1...5 {
     love()
   }
 }

这将记录如下内容 1

This would log something like this 1

1
1
1
1
2
2
2
2
2

考虑示例,如果它是同步而不是异步,那么输出会是一样的吗?那么在上面使用async有什么用呢?

Thinking about the example, If it was a sync instead of async then the output would've been the same? so what's the use of using async above?

此外,在JS中,我们需要等待.迅速,我们不必做任何事情吗?有人可以通过举一个异步的例子来迅速向我解释吗?

Also, In JS we need to await. In swift we don't have to do anything? Can someone explain it to me by giving example of async and await in swift?

推荐答案

由于两个工作都使用相同的队列,因此第二个异步块将在第一个块结束之前开始执行.不管是异步还是串行.

Since you're using the same queue for both works, the second async block won't start executing until the first block ends. It doesn't matter if it's asynchronous or serial.

如果在两个队列之间添加一条打印语句,您将看到.async.sync之间的真正区别.像这样:

You'll see the true difference between .async and .sync if you add a print statement between both queues. Like this:

queue.async {
    for _ in 1...100 {
          self.smile()
    }
}
print("Finished printing smiles")
queue.async {
    for _ in 1...100 {
          self.love()
    }
}

以前的代码甚至可能在开始打印微笑之前就已经打印了完成的打印微笑"!那是因为异步工作会立即返回并继续执行代码.

The previous code will probably print "Finished printing smiles" even before it has started printing smiles! That's because async work returns instantly and keeps executing code.

让我们看看如果使用同步队列更改队列会发生什么情况

And let's see what happens if you change the queues with synchronous queues:

queue.sync {
    for _ in 1...100 {
          self.smile()
    }
}
print("Finished printing smiles")
queue.sync {
    for _ in 1...100 {
          self.love()
    }
}

是的.现在,同步队列在关闭完成之前等待,然后继续.因此,您将获得100个微笑,然后是完成的打印微笑".

Yup. Now the sync queue waits before the closure completes before carrying on. So, you'll get 100 smiles, and then the "Finished printing smiles".

如果要实现并发,就是这样,两个代码块同时执行(但不能完全同时执行,因为那是并行性),则必须使用两个不同的队列,或指定队列配置中的参数:

If you want to achieve concurrency, that's it, two blocks of code executing simultaneously (but not at exactly the same time, because that would be parallelism), you'll have to use two different queues, or specify the .concurrent parameter in the queue configuration:

override func viewDidLoad() {

    let queue = DispatchQueue(label: "SerialQueue")
    let queue2 = DispatchQueue(label: "AnotherQueue")

    queue.async {
        for _ in 1...100 {
              self.smile()
        }
    }
    print("Finished printing smiles")
    queue2.async {
        for _ in 1...100 {
              self.love()
        }
    }
}

您将看到,这里的顺序比较混乱,并且在执行之间会有所不同.这是因为两个队列同时运行.

As you'll see, the order here is chaotic and will vary between executions. That's because both queues are running at the same time.

此代码的另一个等效项是:

Another equivalent to this code would be:

let queue = DispatchQueue(label: "ConcurrentQueue", attributes: .concurrent)

queue.async {
    for _ in 1...100 {
          self.smile()
    }
}
print("Finished printing smiles")
queue.async {
    for _ in 1...100 {
          self.love()
    }
}

这篇关于iOS中的同步与异步队列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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