preface
Operation
Synchronize Operation using tutorial
Override the main() method.
Asynchronous Operation tutorial
Refer to this Blog.
Asynchronous operations for writing concurrent solutions in Swift
Summary: Rewrite
- isExecuting
- isFinished
- isAsynchronous
- start()
These properties are overridden to manually control the lifecycle of the asynchronous Operation. Instead of running main, the op finishes.
The Finish property directly affects the Op that depends on it.
B depends on A. A’s Finish attribute, KVO, triggers B to run.
How are dependencies between Operations implemented
B.a ddDependency (A).
Add 1.
Add A to B’s dependent array.
2.A The execution is complete
When A is finished, finish = YES, go back to an array, the array holds all dependent Op. The Op will immediately start executing its own start function if it only relies on A. The Op only depends on count-1 if it also depends on other Op’s.
barrier Operation
Implementation of barrier Operation call timing
Add 10 ops to the queue and add barrier operations. How to ensure that bOp is executed after 10 Op?
Barrier Operation rely on 10 Op. 10 Op is completed, will naturally perform the barrier Operation.
How does a barrier Operation block other operations
All ops added to a queue depend on an array. If you add a bOp to the Barrier Operation list, you add a bOp to the bOp list. The Op must be executed after all the dependent BOPs have been executed.
Op.adddependency ([barrier Operation list]).
OperationQueue
How to implement priority scheduling for Operations
There are six priority lists in the OperationQueue, plus one overall list.
Each Operations added to the OperationQueue is assigned to a different linked list due to its priority.
On each execution, the list is traversed from high priority to low priority, so the high priority op is executed first.
Operation Changes the priority
ChangePriority.
- Removed from the original priority list.
- Add to the end of the new priority list.
How does OperationQueue control concurrency
Count +1 for each op executed by OperationQueue. An op finish, count -1.
If Max = 8, current count = 8. Will no longer dispatch Op. If the count is less than Max, send Op.
Which GCD apis are used for OperationQueue, Operation
DispatchWorkItem: The start of Operation is encapsulated in the WorkItem
DispatchQueue: for dispatch of tasks, asynchronous call workitem.perform ().
Use and implementation of waitUntilFinished
WaitUntilFinished = true, NSCondition blocks the current thread.
The blocked Op cannot continue until the NSCondition broadcasts when the Op finishes.
Internal task dispatch mechanism of OperationQueue (Big Problem)
The advanced
What happens between Operation addition and execution
Add to queue
- According to Qos, Operation is encapsulated into DispatchWorkItem as its own attribute for convenient distribution.
- Operation is assigned to a linked list with corresponding priorities based on Qos levels.
perform
- The OperationQueue assigns tasks concurrently, starting with the list with the highest priority.
- Execute the encapsulated DispatchWorkItem asynchronously if the maximum number of concurrent requests is not exceeded.
Higher-order Operation usage
Resolve the hell callback
A normal network request
1. Network request 2. Request callback, parse JSON, convert to Model 3. Finally, call the complete functionCopy the code
This is the most common. If the business is more complex, there can be a lot of nested callbacks. Hell callback for short.
High order usage, perfect solution layer nesting, callback nesting problem. ⚠️ : Errors need to be handled within each OP. If the network request OP returns an error. End parsing immediately. Complete OP also needs to handle interpretation failures. In general, the hell callback is solved.
1. Network request OP 2. Parse JSON, convert to model OP 3. Finally call the complete function OP. 2 depends on 1. 3 depends on 2. These three ops can be run in an OperationQueue.Copy the code
Resolving Network Latency
- Let’s say you have A business, go to page A and make A Network request to get some data (defined as an OP Network).
- However, the display content of A’s page exists locally and has been loaded and rendered. It’s a TableView.
- You click cell 1 (defined as OP click). The network request has not been returned at this time. What should you do?
At this point, you can use OP. Dependencies: OP click depends on OP Network. When the OP Network request comes back, the OP click will be triggered. If you don’t use op, it’s hard to deal with clicks at this point and you have to save them. Wait for the request to come back and see if there’s any pending business. The OperationQueue helps you implement the operation premise and stores the operation. Excellent!
Operation UI
Is it necessary to encapsulate UI operations as operations? Most of the business is unnecessary. When do you use Operation UI? The UI operation depends on some data that is not available yet. Operation UI relies on Operation asynData Get to encapsulate UI operations as Op. The previous example
Blog
Use of asynchronous Operations
WWDC
Advanced NSOperations
Wwdc2015 226 source
Source code version
Operation source code version 2020_Feb_16