The original address: ali-akhtar.medium.com/concurrency…

This is the third part of the Swift concurrent programming series. Contains the following contents:

  1. The state of the Operations
  2. Different kinds of Operations
  3. How to cancel Operations
  4. What is Operation Queue
  5. How to use Operation Queue to manage Operations
  6. Dependency management for Operations
  7. 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:

  1. instantiatedIs just created, and will be transferred to after the creation is completeisReadyState.
  2. The execution of thestartMethod will go toisExecutingstate
  3. After the task is complete, go toisFinishedstate
  4. And before the job is done,cancelIs called, will go toisCancelledstate

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 ifBlockOperationThere 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 addOperationCompleted tasks that he will execute after all tasks are executed concurrently:

NSInvocationOperation

inObjective-CIn theNSInvocationOperationinSwiftCannot be used in. Because it depends onNSInvocationAnd the relatedapiinSwiftIs not available.

Custom Operations

The customOperationsComplete control of the state can be achieved. Below, our customOperationOverwrite themainMethod to implementThe concurrenttheOperation. 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

  1. Operation QueuesIs based onGCDThe high-level abstraction of.
  2. useOperation QueuesTo bring into playOperationsReal strength. You don’t have to call it manuallystartMethod, and instead add it toQueueIn the.
  3. Operation QueuesIs an object-oriented way to manage tasks that need to be executed asynchronously.

The use of Operation the Queues

As shown below, we takeBlockThe way to create twoOperationAnd add them toQueueIn the. And then these twoOperationAll are started and executed in background threads. When we putOperationAdded to theQueueIt will be executed as soon as possible.

If you want to perform multiple tasks sequentially, simply addmaxConcurrentOperationCountSet 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 groupThe effect of waiting for multiple tasks to complete and continuing with previous tasks is realized. We can use it here as wellOperation QueueTo 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:

  1. To create aOperation Queue
  2. Create three tasks
  3. A task is continued and cannot be executed until the previous task is complete
  4. willblockOperations4Set the dependency to the previous three tasks
  5. waitUntilFinishedBlocks the current thread until all tasks are complete. Here we pass infalse.

Advantages of Operation Queues compared with GCD

  1. Compared to GCD, you can easily set task dependencies
  2. KVO can be used to monitor task execution status
  3. Mission support suspended, cancelled. Tasks submitted using the GCD cannot be cancelled
  4. Easier control of the number of concurrent requests