iOS custom nsoperation added to Nsoperationqueue, the system automatically executes the Start method

Source: Internet
Author: User

1. Created 3 tasks to join the queue to execute

-(void) Createoperationqueue
{
    //Create a queue
    nsoperationqueue *queue = [[Nsoperationqueue alloc]init];
    
    Create a task
    ljoperation *op1 = [[Ljoperation alloc]init];
    Op1.namestr = @ "OP1";
    
    Ljoperation *OP2 = [[Ljoperation alloc]init];
     Op2.namestr = @ "OP2";
    
    Ljoperation *OP3 = [[Ljoperation alloc]init];
     Op3.namestr = @ "OP3";
    
    NSLog (@ "block----%@", [Nsthread CurrentThread]);
    
    Adding a task to the queue is equivalent to calling the task's Start method
    //If we want Ljoperation to execute some method, we just need to rewrite the Start method (the system will automatically adjust the Start method)
    [queue ADDOPERATION:OP1];
    [Queue addoperation:op2];
    [Queue addoperation:op3];
}


Look at the results of the console printing;

2017-07-07 22:49:04.195 iosnetworkdemo[4452:393020] __function__start_op1

2017-07-07 22:49:04.195 iosnetworkdemo[4452:393051] __function__start_op3

2017-07-07 22:49:04.195 iosnetworkdemo[4452:393022] __function__start_op2


from the results above it can be seen that OP1 executes first, then OP3, then OP2, not executed in order.

that's why. Because when we add operation to the queue, it is equivalent to handing the operation over to the system, and then the system determines its order of execution.
The custom nsoperation above

@interface ljoperation:nsoperation

//casually passing in a name, as Operation distinguishing
@property (nonatomic,copy) nsstring *namestr;

@end

#import "LJOperation.h"

@implementation ljoperation

//Rewrite the Start method of Nsoperation, where you write down the method code you want to execute
-(void Start
{
    //do something
    NSLog (@ "%s", __function__);
    NSLog (@ "__function__start_%@", self.namestr);
}


-(void) main
{
   NSLog (@ "%s", __function__);
  NSLog (@ "__function__main_%@", self.namestr);
}


Related Article

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.