The original address: ali-akhtar.medium.com/concurrency…
This is the third part of the Swift concurrent programming series. Contains the following contents:
- The state of the Operations
- Different kinds of Operations
- How to cancel Operations
- What is Operation Queue
- How to use Operation Queue to manage Operations
- Dependency management for Operations
- The difference between Operations and GCD
Operations
Operations encapsulates tasks in an object-oriented manner, facilitating asynchronous execution. It can be used in conjunction with Operation Queue or alone.
The Operation class is an abstract base class, and you must use its subclasses to complete tasks. It defines some requirements that you must implement in the base class. In addition, the Foundation framework provides two implementations that you can use directly.
The status of Operations changed
An Operation can be understood as a state machine and represents its lifecycle:
instantiated
Is just created, and will be transferred to after the creation is completeisReady
State.- The execution of the
start
Method will go toisExecuting
state - After the task is complete, go to
isFinished
state - And before the job is done,
cancel
Is called, will go toisCancelled
state
BlockOperation
This is a system-provided class for executing one or more blocks concurrently. Since multiple blocks can be executed, BlockOperation has a group-like feature. BlockOperation is completed only after all blocks have been executed. Advanced features are supported in BlockOperation, such as dependencies, KVO, notifications, and cancellations.
In the figure below, we expect it to execute asynchronously. But the bad news is that it will block the main thread until the task is complete, because we called it on the main threadoperation.start()
It should be noted that ifBlockOperation
There are multiple tasks waiting to be executed, and they execute concurrently, but still block the calling thread. The diagram below:
If we call in another threadstart()
Method, which also blocks:
We can addOperation
Completed tasks that he will execute after all tasks are executed concurrently:
NSInvocationOperation
inObjective-C
In theNSInvocationOperation
inSwift
Cannot be used in. Because it depends onNSInvocation
And the relatedapi
inSwift
Is not available.
Custom Operations
The customOperations
Complete control of the state can be achieved. Below, our customOperation
Overwrite themain
Method to implementThe concurrent
theOperation
. It also blocks its calling thread.
If you plan to use Operation independently and want it to execute asynchronously, there is additional work to be done. We will elaborate on this in the next section.
Operation Queues
Operation Queues
Is based onGCD
The high-level abstraction of.- use
Operation Queues
To bring into playOperations
Real strength. You don’t have to call it manuallystart
Method, and instead add it toQueue
In the. Operation Queues
Is an object-oriented way to manage tasks that need to be executed asynchronously.
The use of Operation the Queues
As shown below, we takeBlock
The way to create twoOperation
And add them toQueue
In the. And then these twoOperation
All are started and executed in background threads. When we putOperation
Added to theQueue
It will be executed as soon as possible.
If you want to perform multiple tasks sequentially, simply addmaxConcurrentOperationCount
Set to1
. As follows:
MaxConcurrentOperationCount limits the number of Operation and in the Queue, the default value is 1, which means that determined by the largest number of concurrent systems.
Dependency management
In the figure below, we can create a sequential execution effect by adding dependencies between the two tasks.
Implement Dispatch groups using Operations Queues
In the previous section, we usedGCD dispatch group
The effect of waiting for multiple tasks to complete and continuing with previous tasks is realized. We can use it here as wellOperation Queue
To implement.
As shown in the figure above, we have three tasks and want them to be executed concurrently, and then the other tasks. Here we did the following:
- To create a
Operation Queue
- Create three tasks
- A task is continued and cannot be executed until the previous task is complete
- will
blockOperations4
Set the dependency to the previous three tasks waitUntilFinished
Blocks the current thread until all tasks are complete. Here we pass infalse
.
Advantages of Operation Queues compared with GCD
- Compared to GCD, you can easily set task dependencies
- KVO can be used to monitor task execution status
- Mission support suspended, cancelled. Tasks submitted using the GCD cannot be cancelled
- Easier control of the number of concurrent requests