Concurrency by Tutorials Flashcards

(35 cards)

1
Q

What is concurrency?

A

the decomposability property of a program, algorithm, or problem into order-independent units

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Why use concurrency?

A

Critical to ensure that your app runs smoothly as possible and that the end user is not ever forced to wait.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What are 2 ways to use concurrency in iOS?

A
  1. Grand Central Dispatch

2. Operation class

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is the purpose of GCD?

A

Queue up tasks - method or closure - that can be run in parallel.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is a synchronous task?

A

Your app will wait and block the current run loop until execution finishes before moving to the next task

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is an asynchronous task?

A

App will start the task but return execution to your app immediately

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is a serial queue?

A

Only have a single thread associated with them and thus only a single task can be executed at a given time

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is a concurrent queue?

A

Utilize as many threads as the system has resources for

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What are some advantages of splitting your app into multiple threads?

A
  1. Faster execution
  2. Responsiveness
  3. Optimized resource consumption
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

How do you create a serial dispatch queue in swift?

A

let queue = DispatchQueue(label: “”)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Are dispatch queues concurrent or serial by default?

A

Serial

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

How do you create a global QoS concurrent queue?

A

let queue = DispatchQueue.global(qos: .userInteractive)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

How do you create a concurrent dispatch queue in swift?

A

let queue = DispatchQueue(label: “”, attributes: .concurrent)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

List the 6 quality of services classes for global concurrent queues Apple offers? (highest to lowest priority)

A

1) .userInteractive
2) .userInitiatted
3) .utility
4) .background
5) .default
6) .unspecified

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

How to create your own concurrent queue with utility QoS?

A

let queue = DispatchQueue(label: “”, qos; .utility, attributes: .concurrent)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

When should you use the .userInteractive QoS?

A

Tasks that the user directly interacts with. UI-updating calculations, animations, etc.

17
Q

When should you use the .userInitiated QoS?

A

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)

18
Q

When should you use the .utility QoS?

A

Tasks that would typically include a progress indicator. Long-running computations, I/0, networking, data feeds

19
Q

When should you use the .background QoS?

A

Tasks that the user is not directly aware of. No user interaction, not time-sensitive. Prefetching, DB maintenance, backups

20
Q

When should you use the .default QoS?

A

This default value of qos argument. Falls between .userInitiated and .utility

21
Q

When to use the .unspecified QoS?

A

Exists to support legacy APIs that may opt-out of a QoS

22
Q

What is a DispatchWorkItem?

A

A class that provides an actual object to hold the code you wish to submit to a queue

23
Q

How do you create and execute DispatchWorkItem?

A
let workItem = DispatchWorkItem {
   print("run code")
}
queue.async(execute: workItem)
24
Q

When would you use a DispatchWorkItem instead of just using anonymous closure?

A

If you have need to cancel the task before or during execution

25
How do you identify another DispatchWorkItem that should be executed after the current work item completes?
DispatchWorkItem's notify(queue:execute:) method
26
What is DispatchGroup class used for?
When you want to track the completion of a group of tasks
27
Give an example of using a group in code that updates a text label after the group is completed
``` let group = DispatchGroup() queue.async(group: group) { ... } group.notify(queue: DispatchQueue.main) { self?.textLabel.text = "Job Done!" } ```
28
True or False: All jobs submitted to a group must be on the same queue?
False: jobs can be submitted on multiple queues
29
How can you synchronously wait on a group to finish?
``` let group = DispatchGroup() group.wait(timeout:) ```
30
What is DispatchGroup's group.enter() method used for?
Lets the dispatch group know that there's another block of code running, which should be counted towards group overall completion
31
What is DispatchGroup's group.leave() method used for?
Lets the group know that corresponding group.enter() has finished
32
Example of queue and group that use enter() and leave()
queue.async(group: group) { group.enter() someAsyncMethod { defer { group.leave() } } }
33
What is DispatchSemaphore used for?
Specify how many concurrent accesses to the resource are allowed
34
How do you request permission for resource with DispatchSemaphore?
semaphore.wait()
35
How do you notify that resource count should be decremented?
semaphore.signal()