First, Introduction
TwoNSOperationSub-class
-
NSOperationis an abstract class and does not have the ability to encapsulate operations, it must use its subclasses
-
NSOperationThere are 3 ways to use subclasses
NSInvocationOperation
NSBlockOperation
- Customizing subclass Inheritance
NSOperation , implementing the corresponding method internally
Third, the specific use of 1.NSInvocationOperationThe use
- Creating
NSInvocationOperation Objects
-(id)initWithTarget:(id)target selector:(SEL)sel object:(id)arg;
- Invoking a
start method to begin an operation
-(void)start;
The method that is called once the operation is performed target sel
Attention:
-By default, when a method is called, it start does not open a new thread to perform the operation, but instead performs the operation synchronously on the current thread
-The operation is performed asynchronously only if NSOperation it is placed NSOperationQueue in one
2.NSBlockOperationThe use
- Creating
NSBlockOperation Objects
+(id)blockOperationWithBlock:(void (^)(void))block;
addExecutionBlock:add more actions by method
addExecutionBlock:(void (^)(void))block;
Attention:
NSBlockOperationoperations are performed asynchronously as long as the encapsulated operand > 1
3.NSOperationQueueThe use
-(void)addOperation:(NSOperation *)op;-(void)addOperationWithBlock:(void (^)(void))block;
4. Maximum number of concurrent
- What is the concurrency number?
The number of simultaneous tasks, for example, 3 threads executing 3 tasks concurrently, the number of concurrency is 3
- The correlation method of maximum concurrency number
-(NSInteger)maxConcurrentOperationCount;-(void)setMaxConcurrentOperationCount:(NSInteger);
5. Cancellation, suspension, recovery of the queue
- Cancel all operations on a queue
-(void)cancelAllOperations;
Note: You can also call NSOperation a -(void)cancel method to cancel a single operation
- pausing and resuming queues
-(void)setSuspended:(BOOL)b;// YES 代表暂停队列,NO 代表恢复队列-(BOOL)isSuspended;
Queue type of 6.GCD
- Concurrent queues
- Created by yourself
- Global
- Serial queue
- Primary queue
- Created by yourself
Queue type of 7.NSOperationQueue
- Primary queue
- [Nsoperationqueue Mainqueue]
- All tasks added to the main queue (nsoperation) are placed in the main thread to execute
- Non-Home row (other queues)
- [[Nsoperationqueue alloc] init]
- It also includes: Serial, concurrency function
- Tasks that are added to this queue (nsoperation) are automatically placed in child threads to execute
8.NSOperation Other usage
-
Operation Dependent
NSOperationDependencies can be set to guarantee the order of execution, for example, you must let Operation A execute, before you can perform operation B, so to write
[operation addDependency:operationA]; //操作 B 依赖于操作 A
- You can
queue create a NSOperation dependency between different
Note: cannot depend on each other, for example, a relies on b,b dependency a
- Monitoring of operations
- Can listen for an operation completed
-(void (^)(void))completionBlock;-(void)setCompletionBlock:(void (^)(void))block;
-
Custom-DefinedNSOperation
- The custom
NSOperation step is simple, overriding the -(void)main method, which implements the task that you want to perform
-
-(void)mainnote points for overriding methods
- Create an auto-free pool yourself (because if it is an asynchronous operation, you cannot access the main thread's auto-free pool)
- Frequently
-(void)isCancelled detects whether an operation is canceled by means of a method and responds to a cancellation
-
Custom NSOperation Download Picture ideas-no sandbox cache
- Custom
NSOperation Download Picture Ideas-There is a sandbox cache
IOS Multithreading-Nsoperation