A: Introduction
The role of 1.NSOperation:
With Nsoperation and Nsoperationqueue can also realize multithreading.
2.NSOperation and nsoperationqueue the steps to implement multithreading:
2.1 Now encapsulate the required actions into the Nsoperation object
2.2 Then add the nsoperation to the Nsoperationqueue
2.3 The system will automatically remove the nsoperation from the Nsoperationqueue
II: Subclasses of the Nsoperation
1.NSOPeration is an abstract class that does not have the ability to encapsulate operations and must use his subclasses.
2. There are three ways to use the Nsoperation subclass:
2.1NSInvocationOpertation
2.2NSBlockOperating
2.3 Customizing subclass Inheritance Nsoperation, implementing internal corresponding methods
Three: Nsoperationqueue
Type of 1.NSOperationQueue:
1.1 Main queue, via [nsoperationqueue mainqueue]; available.
1.2 Other queues: Serial queue, concurrent queue. Available through [[nsoperationqueue alloc ] init];
2. Use:
- (void) Touchesbegan: (Nsset<uitouch *> *) touches withevent: (Uievent *)Event{nsoperationqueue*OPQ =[[Nsoperationqueue alloc] init]; Nsinvocationoperation*OP1 = [[Nsinvocationoperation alloc] initwithtarget:self selector: @selector (RUN1)Object: nil]; Nsinvocationoperation*OP2 = [[Nsinvocationoperation alloc] initwithtarget:self selector: @selector (RUN2)Object: nil]; [OpQ ADDOPERATION:OP1];//The [OP1 start] method is called internally and placed in the sub-line range[OpQ addoperation:op2];}- (void) run1{NSLog (@"1------%@", [Nsthread CurrentThread]);}- (void) run2{NSLog (@"2------%@", [Nsthread CurrentThread]);}
3. Custom operation
3.1 Usage Scenarios: When the operation is more complex, you can customize a nsoperation, and then put the complex operations in the custom operation.
3.2 Implementations:
3.2.1 needs to override the main function in the custom operation:
Such as:
-(void) main{ NSLog (@ "%@-----------------custom Operation", [ Nsthread CurrentThread]);}
3.2.2 Create a custom operation to add to the queue:
Zznsopertation *op3 = [[zznsopertation alloc] init];
[OpQ addoperation: op3];
3.2.3 Printing results:
2016-01-19 17:43:39.941 nsperationqueue[16438:1477860] <nsthread:0x7ff00a60d600>{number = 4, name = (NULL)}----- ------------Custom Operation
2016-1-19 Preliminary study on Nsoperation