0
0
Swiftprogramming~5 mins

Task for launching concurrent work in Swift - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is a Task in Swift concurrency?
A Task represents a unit of asynchronous work that runs concurrently. It allows you to start work that can run independently from the current code flow.
Click to reveal answer
beginner
How do you create a new concurrent task in Swift?
You create a new task by calling Task { ... } and placing asynchronous code inside the closure. This runs the code concurrently.
Click to reveal answer
intermediate
What is the difference between Task and async/await?
async/await lets you write asynchronous code that waits for results, while Task launches work concurrently without blocking the current thread.
Click to reveal answer
intermediate
How can you cancel a running Task?
You can call task.cancel() on the Task instance. Inside the task, you should check for cancellation using Task.isCancelled to stop work cooperatively.
Click to reveal answer
beginner
What happens if you call Task { ... } inside a synchronous function?
The task starts running concurrently in the background immediately, allowing the synchronous function to continue without waiting for the task to finish.
Click to reveal answer
Which Swift code launches a new concurrent task?
ATask { await fetchData() }
BfetchData()
Casync fetchData()
DDispatchQueue.main.async { fetchData() }
How do you check if a task has been cancelled inside its code?
Aif task.isCancelled { ... }
Bif Task.isCancelled { ... }
Cif cancelled { ... }
Dif Task.cancelled { ... }
What does Task { ... } return?
AAn async function
BVoid
CA DispatchQueue
DA Task instance representing the concurrent work
Which statement is true about Task in Swift concurrency?
AIt runs code concurrently without blocking the current thread
BIt blocks the current thread until the task finishes
CIt only runs on the main thread
DIt is used to create synchronous functions
How can you cancel a running Task?
ASet a flag variable to false
BCall <code>stop()</code> on the Task instance
CCall <code>cancel()</code> on the Task instance
DYou cannot cancel a Task once started
Explain how to launch concurrent work using Task in Swift and how cancellation works.
Think about starting work that runs alongside your current code and how you can stop it if needed.
You got /4 concepts.
    Describe the difference between using async/await and launching a Task in Swift concurrency.
    Consider when you want to wait for a result versus when you want to start work and continue immediately.
    You got /4 concepts.