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);
}