dispatch_barrier_async equivalent in Swift 3

2019-03-09 09:33发布

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)
    //...
  }
}

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

1条回答
家丑人穷心不美
2楼-- · 2019-03-09 10:02

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

func subscribe(subscriber: DaoDelegate) {
  (self.subscribers.q).async(flags: .barrier) { 
    //...
  }
}
查看更多
登录 后发表回答