FREE TRIAL: Accelerate your app development career with Hacking with Swift+! >>

What are tasks and task groups?

Paul Hudson    @twostraws   

Using async/await in Swift allows us to write asynchronous code that is easy to read and understand, but by itself it doesn’t enable us to run anything concurrently – even with several CPU cores working hard, async/await code would still execute sequentially.

To create actual concurrency – to provide the ability for multiple pieces of work to run at the same time – Swift provides us with two specific types for constructing and managing concurrency in a way that makes it easier to use: Task and TaskGroup. Although the types themselves aren’t complex, they unlock a lot of power and flexibility, and sit at the core of how we use concurrency with Swift.

Which you choose – Task or TaskGroup – depends on the goal of your work: if you want one or two independent pieces of work to start, then Task is the right choice. If you want to split up one job into several concurrent operations then TaskGroup is a better. Task groups work best when their individual operations return exactly the same kind of data, but with a little extra effort you can coerce them into supporting heterogenous data types.

Although you might not realize it, you’re using tasks every time you write any async code in Swift. You see, all async functions run as part of a task whether or not we explicitly ask for it to happen. Even using async let is syntactic sugar for creating a task then waiting for its result – special syntax that makes a particular piece of code easier to write. This is why if you use multiple sequential async let calls they will all start executing immediately while the rest of your code continues.

Both Task and TaskGroup can be created with one of four priority levels: high is the most important, then default, low, and finally background at the bottom. Task priorities allow the system to adjust the order in which it executes work, meaning that important work can happen before unimportant work.

Tip: If you’ve been doing iOS programming for a while, you can also use the more familiar priority names of userInitiated and utility in place of high and low respectively. There is no equivalent to the old userInteractive priority, which is now exclusively reserved for the user interface.

Hacking with Swift is sponsored by Essential Developer

SPONSORED From August 2nd to 8th you can join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer!

Save your spot now

Sponsor Hacking with Swift and reach the world's largest Swift community!

BUY OUR BOOKS
Buy Pro Swift Buy Swift Design Patterns Buy Testing Swift Buy Hacking with iOS Buy Swift Coding Challenges Buy Swift on Sundays Volume One Buy Server-Side Swift (Vapor Edition) Buy Advanced iOS Volume One Buy Advanced iOS Volume Two Buy Advanced iOS Volume Three Buy Hacking with watchOS Buy Hacking with tvOS Buy Hacking with macOS Buy Dive Into SpriteKit Buy Swift in Sixty Seconds Buy Objective-C for Swift Developers Buy Server-Side Swift (Kitura Edition) Buy Beyond Code

Was this page useful? Let us know!

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.