IOS Multithreading-Nsoperation

Source: Internet
Author: User

First, Introduction
    • NSOperationThe role
      NSOperationIs the right GCD layer of encapsulation, more object-oriented.
    • NSOperation NSOperationQueue Multi-threaded programming is also possible with the use of.

    • NSOperationand NSOperationQueue the specific steps to implement multithreading

    • Encapsulate the action that needs to be performed NSOpertion in an object first
    • The Nsoperation object is then added to the NSOperationQueue
    • The system will automatically NSOperationQueue remove the NSOperation
    • Put the extracted NSOperation encapsulated operation into a new thread to execute
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
    • NSOperationQueueThe role

      • NSOperationQueueMethods can be called start to perform tasks, but default is synchronous execution
      • If it is NSOperation added to NSOperationQueue (the operations queue), the system automatically executes NSOperation the operation in the asynchronous
    • Add action to NSOperationQueue medium

-(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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.