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?
dispatch_barrier_sync(queue,void(^block)()) executes all the task blocks added before barrier in queue, then executes the block of barrier task, and then executes the task blocks added after barrier.
Overview. Use a barrier to synchronize the execution of one or more tasks in your dispatch queue. When you add a barrier to a concurrent dispatch queue, the queue delays the execution of the barrier block (and any tasks submitted after the barrier) until all previously submitted tasks finish executing.
The async()
method has a flags
parameter which accepts a .barrier
option:
func subscribe(subscriber: DaoDelegate) { (self.subscribers.q).async(flags: .barrier) { //... } }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With