Swift 3中的dispatch_barrier_async等效项 [英] dispatch_barrier_async equivalent in Swift 3

查看:396
本文介绍了Swift 3中的dispatch_barrier_async等效项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

重构同事的代码,我正在快速3中寻找dispatch_barrier_async的等效项.有很多队列在起作用,他的设计是仅阻塞此队列,并且仅针对此单个操作.

Refactoring a colleague's code, and I'm looking for the equivalent of dispatch_barrier_async in swift 3. There are a lot of queues at play, and his design is to block only this queue, and only for this single operation.

// Swift 2.3
func subscribe(subscriber: DaoDelegate) {
  dispatch_barrier_async(self.subscribers.q) { // NOTE: barrier, requires exclusive access for write
    //...
  }
}

// Swift 3 
func subscribe(subscriber: DaoDelegate) {
  (self.subscribers.q).async { // (Not equivalent, no barrier on the concurrent queue)
    //...
  }
}

我可以在Swift 3中保持相同的功能而无需重构所有队列类型吗?

Can I keep that same functionality in Swift 3 without refactoring all the queue types?

推荐答案

async()方法具有一个flags参数,该参数接受.barrier 选项:

The async() method has a flags parameter which accepts a .barrier option:

func subscribe(subscriber: DaoDelegate) {
  (self.subscribers.q).async(flags: .barrier) { 
    //...
  }
}

这篇关于Swift 3中的dispatch_barrier_async等效项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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