iOS multithreading -03-nsoperation and Nsoperationqueue

Source: Internet
Author: User

Brief introduction
    • Multithreading can also be achieved through the combination of nsoperation and Nsoperationqueue
    • Tasks are typically encapsulated into Nsoperation objects, and objects are added to Nsoperationqueue to implement
Nsoperation
  • Nsoperation is an abstract class that cannot be used to encapsulate operations directly, typically using its subclasses to encapsulate operations
  • If you do not add the Nsoperation object to the Nsoperationqueue, the operation will only be performed on the current thread, and you will need to start the operation manually

  • Subclass of Nsoperation

    • Nsinvocationoperation

      //创建操作NSInvocationOperation *operation = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(run) object:nil];//开始执行操作[operation start];
    • Nsblockoperation (asynchronous execution when operand is greater than 1 o'clock)

      //创建操作NSBlockOperation *operation = [NSBlockOperation blockOperationWithBlock:^{    //操作封装的任务代码}];//添加操作[operation addExecutionBlock:^{    //操作封装的任务代码}];//开始执行操作[operation start];
    • To customize a subclass

      • Override-(void) Main method, which encapsulates the action to be performed in the method
      • In the-(void) Main method, you need to set up an auto-free pool yourself (cause: an asynchronous operation that cannot access the main thread's auto-free pool)
      • Check the status of the operation by checking the properties such as cancelled, and perform the corresponding operation
  • Callback when the set operation completes

    //创建操作NSBlockOperation *operation1 = [NSBlockOperation blockOperationWithBlock:^{   //任务代码}];//设置回调operation.completionBlock = ^{    //操作完成时执行的代码};
Nsoperationqueue
  • Role

    • accommodate actions that need to be performed asynchronously
    • The system automatically performs the actions in the nsoperationqueue and does not need to be started manually
  • Related Operations for Nsoperationqueue

    • Create an action queue

      • [[Nsoperationqueue alloc] init]
    • Get the main action queue

      • + (Nsoperationqueue *) mainqueue
    • Gets the current Operation queue

      • + (Nullable Nsoperationqueue *) currentqueue
    • Adding operations to the queue

      • -(void) Addoperation: (Nsoperation *) op
      • -(void) Addoperationwithblock: (void (^) (void)) block
    • Setting the maximum number of concurrent operations

      • -Setmaxconcurrentoperationcount: (Nsinteger) Count
      • If the number of concurrent operations is 1, the serial queue
    • pausing/starting a task in a queue

      • -setsuspended: (BOOL) b
    • Cancel a task in a queue

      • -(void) cancelalloperations
      • The queue will not be suspended until the executing task has finished executing
Steps to implement multithreading
    • Create an action queue

      NSOperationQueue *queue = [[NSOperationQueue alloc] init]
    • Adding operations to the queue

      /**方法一*///创建操作NSInvocationOperation *operation1 = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(download) object:nil] //download为操作封装的任务方法NSBlockOperation *operation2 = [NSBlockOperation blockOperationWithBlock:^{    //任务代码}];//添加操作[queue addOperation:operation1];[queue addOperation:operation2];/**方法二*///添加操作[queue addOperationWithBlock:^{    //操作封装的任务}];
Operation Dependent
    • Role

      • Establish the order of execution relationships between operations
      • Operation dependencies are action-oriented and can establish dependencies between operations across queues
      • To establish a dependency between operations, you must establish the operation before it is added to the queue
    • Steps to establish an operation dependency

      • Create an action queue

        NSOperationQueue *queue = [[NSOperationQueue alloc] init]
      • Add action

        NSBlockOperation *operation1 = [NSBlockOperation blockOperationWithBlock:^{    //任务1代码}];NSBlockOperation *operation2 = [NSBlockOperation blockOperationWithBlock:^{    //任务2代码}];
      • Establish dependencies (must be before the operation is added to the queue)

        [operation1 addDependency:operation2];
      • Add an action to a queue

        [queue addOperation:operation1];[queue addOperation:operation2];
Communication between threads
    • From the main thread to the child thread

      • The switchover from the main thread to the child thread is usually done automatically by the system, simply by adding the operation to the queue, and the system will switch to the child thread to perform the operation, depending on the actual need.
    • From a child thread to the main thread

      • Add the required action to the main action queue (the operation will run in the common mode of the main runloop)

        [[NSOperationQueue mainQueue] addOperationWithBlock:^{        //需要在主线程中执行的任务代码    }];

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

iOS multithreading -03-nsoperation and Nsoperationqueue

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.