IOS-multithreading-Introduction to NSOperration and ios-nsoperration
An NSOperation object represents an operation, and the object is equivalent to a block in GCD.
I. Functions of NSOperation:
Using NSOperation and NSOperationQueue together can also achieve multithreading.
2. NSOperation and NSOperationQueue to implement multiple threads:
Step 1: First encapsulate the operation to be executed into an NSOperation object;
Step 2: the system automatically retrieves NSOperation from the NSOperationQueue;
Step 3: Put the obtained NSOperation encapsulated operations in a thread for execution.
Iii. NSOperation subclass
NSOperation is an abstract class and does not have the ability to encapsulate operations. It must be used as a subclass.
4. There are three NSOperation subclass methods: 1> NSInvocationOperation2> NSBlockOperation3> Custom subclass inherits NSOperation to implement internal corresponding methods. 5. Usage: Method 1: NSInvocationOperation, the Code is as follows: Importance level: Demo
1-(void) viewDidLoad 2 {3 [super viewDidLoad]; 4 5 // create a queue 6 NSOperationQueue * queue = [[NSOperationQueue alloc] init]; 7 8 // creation operation 9 NSInvocationOperation * operation = [[NSInvocationOperation alloc] initWithTarget: self selector: @ selector (download) object: nil]; 10 // operation calls start directly, is synchronously executed (the operation is executed in the current thread) 11 // [operation start]; 12 13 // Add the operation to the queue, it will be automatically executed asynchronously (if you do not add operation to the queue, directly [operation start]; if you execute it, it is equivalent to calling [self download], not asynchronous execution, is executed in the main thread. When added to the operation queue, a new thread is started and executed asynchronously.) 14 15 [queue addOperation: operation]; 16} 17 18-(void) download19 {20 NSLog (@ "download ----- % @", [NSThread currentThread]); 21}
Method 2. Use NSBlockOperation. The Code is as follows:
Important level: Understanding (test1 test2 contains understanding)
- (void)viewDidLoad{ [super viewDidLoad]; [self test2];}
-(Void) test2 {NSBlockOperation * operation1 = [NSBlockOperation blockOperationWithBlock: ^ {NSLog (@ "--- download image ---- 11 --- % @", [NSThread currentThread]);}]; [operation1 addExecutionBlock: ^ {NSLog (@ "--- download image ---- 12 --- % @", [NSThread currentThread]) ;}]; NSBlockOperation * operation2 = [NSBlockOperation blockOperationWithBlock: ^ {NSLog (@ "--- download image ---- 2 --- % @", [NSThread currentThread]) ;}]; NSBlockOperation * operation3 = [NSBlockOperation blockOperationWithBlock: ^ {NSLog (@ "--- download image ---- 3 --- % @", [NSThread currentThread]) ;}]; NSBlockOperation * operation4 = [NSBlockOperation blockOperationWithBlock: ^ {NSLog (@ "--- download image ---- 4 --- % @", [NSThread currentThread]) ;}]; // 1. create a queue NSOperationQueue * queue = [[NSOperationQueue alloc] init]; // equivalent to a GCD global concurrency queue, the following four operations are asynchronously executed in the newly opened thread // main queue column // NSOperationQueue * queue = [NSOperationQueue mainQueue]; // equivalent to the GCD main queue column, the following four operations are performed in the main queue. // 2. add the operation to the queue (automatic asynchronous execution) [queue addOperation: operation1]; [queue addOperation: operation2]; [queue addOperation: operation3]; [queue addOperation: operation4];}
-(Void) test {NSBlockOperation * operation = [[NSBlockOperation alloc] init]; [operation addExecutionBlock: ^ {NSLog (@ "--- download image ---- 1 --- % @", [NSThread currentThread]);}]; [operation addExecutionBlock: ^ {NSLog (@ "--- download image ---- 2 --- % @", [NSThread currentThread]);}]; [operation addExecutionBlock: ^ {NSLog (@ "--- download image ---- 3 --- % @", [NSThread currentThread]) ;}]; [operation start]; // if the number of tasks is greater than 1, asynchronous execution will start (otherwise, synchronous execution will be performed in the main thread )}
# What to learn ##
-(Void) viewDidLoad {[super viewDidLoad];}-(void) touchesBegan :( NSSet *) touches withEvent :( UIEvent *) event {NSOperationQueue * queue = [[NSOperationQueue alloc] init]; [queue addOperationWithBlock: ^ {// 1. asynchronous download image NSURL * url = [NSURL URLWithString: @ "http://d.hiphotos.baidu.com/image/pic/item/37d3d539b6003af3290eaf5d362ac65c1038b652.jpg"]; NSData * data = [NSData dataWithContentsOfURL: url]; UIImage * ima Ge = [UIImage imageWithData: data]; // 2. return to the main thread and display the image // [self generated mselecw.mainthread: <# (SEL) #> withObject: <# (id) #> waitUntilDone: <# (BOOL) #>]; // dispatch_async (dispatch_get_main_queue (), ^ {//}); [[NSOperationQueue mainQueue] addOperationWithBlock: ^ {self. imageView. image = image;}] ;}] ;}- (void) dependency {/** assume that there are three operations, A, B, and C. The requirements are as follows: 1. all three operations are executed asynchronously. operation C depends on Operation B 3. operation B depends on operation A * // 1. create a queue (non-main queue column) NSOper AtionQueue * queue = [[NSOperationQueue alloc] init]; // 2. create three NSBlockOperation * operationA = [NSBlockOperation blockOperationWithBlock: ^ {NSLog (@ "A1 --- % @", [NSThread currentThread]);}]; // [operationA addExecutionBlock: ^ {// NSLog (@ "A2 --- % @", [NSThread currentThread]); //}]; // [operationA setCompletionBlock: ^ {// NSLog (@ "AAAAA --- % @", [NSThread currentThread]); //}]; NSBlockOperation * ope RationB = [NSBlockOperation blockOperationWithBlock: ^ {NSLog (@ "B --- % @", [NSThread currentThread]);}]; NSBlockOperation * operationC = [NSBlockOperation blockOperationWithBlock: ^ {NSLog (@ "C --- % @", [NSThread currentThread]);}]; // sets the dependency [operationB addDependency: operationA]; [operationC addDependency: operationB]; // 3. add an operation to the queue (automatic asynchronous task execution) [queue addOperation: operationC]; [queue addOperation: operationA ]; [Queue addOperation: operationB];}-(void) maxCount {// 1. create a queue (non-main queue columns will create a new thread) NSOperationQueue * queue = [[NSOperationQueue alloc] init]; // 2. set the maximum concurrency (if set to 3, a maximum of three tasks can be concurrently executed) queue. maxConcurrentOperationCount = 3; // If set to 1, the following tasks are executed in sequence in the new thread. // 3. Add the operation to the queue (automatic asynchronous task execution and concurrency)
// The following five operations are executed asynchronously. If some operations need to be performed in the method, you can implement them in the form of operation 5.
// Operation 1 NSBlockOperation * operation1 = [NSBlockOperation blockOperationWithBlock: ^ {NSLog (@ "Download image 1 --- % @", [NSThread currentThread]);}];
// Operation 2 NSBlockOperation * operation2 = [NSBlockOperation blockOperationWithBlock: ^ {NSLog (@ "Download Image 2 --- % @", [NSThread currentThread]);}];
// Operation 3 NSBlockOperation * operation3 = [NSBlockOperation blockOperationWithBlock: ^ {NSLog (@ "Download picture 3 --- % @", [NSThread currentThread]);}];
// Operation 4 NSBlockOperation * operation4 = [NSBlockOperation blockOperationWithBlock: ^ {NSLog (@ "Download Image 4 --- % @", [NSThread currentThread]);}];
// Operation 5 NSInvocationOperation * operation5 = [[NSInvocationOperation alloc] initWithTarget: self selector: @ selector (download) object: nil]; [queue addOperation: operation1]; [queue addOperation: operation2]; [queue addOperation: operation3]; [queue addOperation: operation4]; [queue addOperation: operation5]; [queue addOperationWithBlock: ^ {NSLog (@ "Download image 5 --- % @", [NSThread currentThread]) ;}]; [queue addOperationWithBlock: ^ {NSLog (@ "Download image 6 --- % @", [NSThread currentThread]) ;}]; [queue addOperationWithBlock: ^ {NSLog (@ "Download image 7 --- % @", [NSThread currentThread]) ;}]; [queue addOperationWithBlock: ^ {NSLog (@ "Download image 8 --- % @", [NSThread currentThread]) ;}]; [queue addOperationWithBlock: ^ {NSLog (@ "Download image 9 --- % @", [NSThread currentThread]) ;}]; [queue cancelAllOperations];}
-(Void) download
{
NSLog (@ "download --- % @", [NSThread currentThread]);
}
-(Void) Cancel {[super didReceiveMemoryWarning]; // [queue cancelAllOperations]; // cancel all tasks in the queue (unrecoverable)}-(void) scrollViewWillBeginDragging :( UIScrollView *) scrollView {// [queue setsuincluded: YES]; // pause all tasks in the queue}-(void) scrollViewDidEndDragging :( UIScrollView *) scrollView willDecelerate :( BOOL) decelerate {// [queue setsuincluded: NO]; // restore all tasks in the queue}-(void) baseUse {// 1. create a queue (non-main queue column) NSOperationQueue * queue = [[NSOperationQueue alloc] init]; // 2. add operations to the queue (automatic asynchronous task execution, concurrency) NSBlockOperation * operation1 = [NSBlockOperation blockOperationWithBlock: ^ {NSLog (@ "Download image 1 --- % @", [NSThread currentThread]);}]; NSBlockOperation * operation2 = [NSBlockOperation blockOperationWithBlock: ^ {NSLog (@ "Download Image 2 --- % @", [NSThread currentThread]);}]; [queue addOperation: operation1]; [queue addOperation: operation2]; [queue addOperationWithBlock: ^ {NSLog (@ "Download Image 3 --- % @", [NSThread currentThread]) ;}]; // concurrent execution of 3 operations}
Vi. Operation dependency:
1. dependencies can be set between NSOperation to ensure the execution sequence. For example, you must have operation A complete before operation B can be executed. You can write it like this:
[OperationB addDependency: operationA]; // operation B depends on operation
2. You can create dependencies between NSOperation of different queue: