- Nsoperation Abstract class
- Nsoperation is an "abstract class" and cannot be used directly
- The use of abstract classes is to define properties and methods common to subclasses
- In Apple's head file, some abstract classes and subclasses are defined in the same header file.
- Sub-class:
Nsinvocationoperation (Call)
Nsblockoperation (Block)
Nsoperationqueue queue
Basic Walkthrough
Nsinvocationoperation
Start
- The Start method executes the @selector method on the current thread
-(void) OpDemo1 {
Nsinvocationoperation *op = [[Nsinvocationoperation alloc] initwithtarget:self selector: @selector (downloadimage:) object:@ "Invocation"];
The Start method executes the @selector method on the current thread
[op start];
}
-(void) Downloadimage: (id) obj {
NSLog (@ "%@%@", [Nsthread CurrentThread], obj);
}
Add to queue
1 Adding an action to the queue will "asynchronously" execute the Selector method
-(void) OpDemo2 {
Nsoperationqueue *q = [[Nsoperationqueue alloc] init];
Nsinvocationoperation *op = [[Nsinvocationoperation alloc] initwithtarget:self selector: @selector (downloadimage:) object:@ "queue"];
[Q Addoperation:op];
}
Add multiple actions
-(void) OpDemo3 {
Nsoperationqueue *q = [[Nsoperationqueue alloc] init];
for (int i = 0; i < ++i) {
Nsinvocationoperation *op = [[Nsinvocationoperation alloc] initwithtarget:self selector: @selector (downloadimage:) object:@ (i)];
[Q Addoperation:op];
}
}
Execution effect: Multiple threads are turned on and not sequentially executed. Same as concurrent queue & asynchronous execution in GCD!
conclusion, in Nsoperation:
- Tasks performed asynchronously with operations
- Queues--Global queues
Nsblockoperation
-(void) OpDemo4 {
Nsoperationqueue *q = [[Nsoperationqueue alloc] init];
Nsblockoperation *op = [Nsblockoperation blockoperationwithblock:^{
NSLog (@ "%@", [Nsthread CurrentThread]);
}];
[Q Addoperation:op];
}
Using block to define the operation, all the code is written together, simpler and easier to maintain!
more simple, add directly Block
-(void) OpDemo5 {
Nsoperationqueue *q = [[Nsoperationqueue alloc] init];
for (int i = 0; i < ++i) {
[Q addoperationwithblock:^{
NSLog (@ "%@%d", [Nsthread CurrentThread], i);
}];
}
}
Add a different action to the queue
-(void) OpDemo5 {
Nsoperationqueue *q = [[Nsoperationqueue alloc] init];
for (int i = 0; i < ++i) {
[Q addoperationwithblock:^{
NSLog (@ "%@%d", [Nsthread CurrentThread], i);
}];
}
Nsblockoperation *OP1 = [Nsblockoperation blockoperationwithblock:^{
NSLog (@ "Block%@", [Nsthread CurrentThread]);
}];
[Q ADDOPERATION:OP1];
Nsinvocationoperation *OP2 = [[Nsinvocationoperation alloc] initwithtarget:self selector: @selector (downloadimage:) object:@ "Invocation"];
[Q ADDOPERATION:OP2];
}
- You can add subclasses of any nsoperation to Nsoperationqueue
Inter-thread communication
-(void) OpDemo6 {
Nsoperationqueue *q = [[Nsoperationqueue alloc] init];
[Q addoperationwithblock:^{
NSLog (@ "time-consuming Operation%@", [Nsthread CurrentThread]);
Main thread Update UI
[[Nsoperationqueue Mainqueue] addoperationwithblock:^{
NSLog (@ "Update UI%@", [Nsthread CurrentThread]);
}];
}];
}
Step 6 Nsoperation (Basic walkthrough)