Concurrency by Tutorials Flashcards
(35 cards)
What is concurrency?
the decomposability property of a program, algorithm, or problem into order-independent units
Why use concurrency?
Critical to ensure that your app runs smoothly as possible and that the end user is not ever forced to wait.
What are 2 ways to use concurrency in iOS?
- Grand Central Dispatch
2. Operation class
What is the purpose of GCD?
Queue up tasks - method or closure - that can be run in parallel.
What is a synchronous task?
Your app will wait and block the current run loop until execution finishes before moving to the next task
What is an asynchronous task?
App will start the task but return execution to your app immediately
What is a serial queue?
Only have a single thread associated with them and thus only a single task can be executed at a given time
What is a concurrent queue?
Utilize as many threads as the system has resources for
What are some advantages of splitting your app into multiple threads?
- Faster execution
- Responsiveness
- Optimized resource consumption
How do you create a serial dispatch queue in swift?
let queue = DispatchQueue(label: “”)
Are dispatch queues concurrent or serial by default?
Serial
How do you create a global QoS concurrent queue?
let queue = DispatchQueue.global(qos: .userInteractive)
How do you create a concurrent dispatch queue in swift?
let queue = DispatchQueue(label: “”, attributes: .concurrent)
List the 6 quality of services classes for global concurrent queues Apple offers? (highest to lowest priority)
1) .userInteractive
2) .userInitiatted
3) .utility
4) .background
5) .default
6) .unspecified
How to create your own concurrent queue with utility QoS?
let queue = DispatchQueue(label: “”, qos; .utility, attributes: .concurrent)
When should you use the .userInteractive QoS?
Tasks that the user directly interacts with. UI-updating calculations, animations, etc.
When should you use the .userInitiated QoS?
When the user kicks off a task from the UI that needs to happen immediately but can be done async. (If a user clicked a button, this is probably the queue you want)
When should you use the .utility QoS?
Tasks that would typically include a progress indicator. Long-running computations, I/0, networking, data feeds
When should you use the .background QoS?
Tasks that the user is not directly aware of. No user interaction, not time-sensitive. Prefetching, DB maintenance, backups
When should you use the .default QoS?
This default value of qos argument. Falls between .userInitiated and .utility
When to use the .unspecified QoS?
Exists to support legacy APIs that may opt-out of a QoS
What is a DispatchWorkItem?
A class that provides an actual object to hold the code you wish to submit to a queue
How do you create and execute DispatchWorkItem?
let workItem = DispatchWorkItem { print("run code") } queue.async(execute: workItem)
When would you use a DispatchWorkItem instead of just using anonymous closure?
If you have need to cancel the task before or during execution