Tuesday, May 16, 2017

All About Concurrency in Swift

Umberto Raimondi:

Swift 3 introduces a new function to perform assertions on the current execution context, that allows to verify if a closure is being executed on the expected queue. We can build predicates using the three enum cases of DispatchPredicate: .onQueue, to verify that we are on a specific queue, .notOnQueue, to verify the opposite and .onQueueAsBarrier to check if the current closure or work item are acting as a barrier on a queue.

Update (2017-06-04): John Sundell:

One common misconception about GCD is that “once you schedule a task it cannot be cancelled, you need to use the Operation API for that”. While this used to be true, with iOS 8 & macOS 10.10 DispatchWorkItem was introduced, which provide this exact functionality in a very easy to use API.

[…]

As you can see above, using DispatchWorkItem is actually a lot simpler and nicer in Swift than having to use a Timer or Operation, thanks to trailing closure syntax and how well GCD imports into Swift. You don’t need @objc marked methods, or #selector, it can all be done with closures.

Comments RSS · Twitter

Leave a Comment