Multithreading in iOS development -- NSOperation and iosnsoperation
This article is based on the top of the teacher's blog to learn, reproduced address: http://www.cnblogs.com/wendingding/p/3809042.htmlI. Introduction to NSOperation
1. Simple Description
NSOperation: Using NSOperation and NSOperationQueue together can also implement multi-thread programming.
The procedure for implementing multiple threads in NSOperation and NSOperationQueue is as follows:
(1) encapsulate the operations to be executed into an NSOperation object.
(2) then add the NSOperation object to the NSOperationQueue
(3) NSOperation in the NSOperationQueue is automatically retrieved.
(4) Place the extracted NSOperation encapsulated operations in the new thread to execute the token.
2. NSOperation subclass
NSOperation is an abstract class and does not have the ability to encapsulate operations. NSOperation must be a subclass of NSOperation.
There are three ways to use the NSOperation annotation class:
(1) NSInvocationOperation
(2) NSBlockOperation
(3) The Custom subclass inherits NSOperation and implements the corresponding internal limit method.
NSInvocationOperation:
Sample Code:
- (void)viewDidLoad { [super viewDidLoad]; NSInvocationOperation *operation = [[NSInvocationOperation alloc]initWithTarget:self selector:@selector(thread1) object:nil]; [operation start]; NSInvocationOperation *operation2 = [[NSInvocationOperation alloc]initWithTarget:self selector:@selector(thread2) object:nil]; [operation2 start]; }-(void)thread1{ NSLog(@"currentThread = %@",[NSThread currentThread]);}-(void)thread2{ NSLog(@"currentThread2 = %@",[NSThread currentThread]);}
currentThread = <NSThread: 0x17406d780>{number = 1, name = main}currentThread2 = <NSThread: 0x17406d780>{number = 1, name = main}
We can see that the new thread is not enabled.
Note: The operation object is executed in the main thread by default. A new thread is enabled only when it is added to the queue. That is, by default, operations that are not placed in the queue are executed synchronously. The operation is performed asynchronously only when NSOperation is put in an NSOperationQueue.
NSBlockOperation:
NSBlockOperation * operation3 = [NSBlockOperation blockOperationWithBlock: ^ {NSLog (@ "operation = % @", [NSThread currentThread]);}]; [operation3 start]; view the printed result: operation = <NSThread: 0x1700728c0> {number = 1, name = main}
Note: As long as the NSBlockOperation encapsulation operand is greater than 1, the operation will be executed asynchronously.
For example:
NSBlockOperation * operation3 = [NSBlockOperation blockOperationWithBlock: ^ {NSLog (@ "operation = % @", [NSThread currentThread]);}]; [operation3 addExecutionBlock: ^ {NSLog (@ "operation2 = % @", [NSThread currentThread]);}]; [operation3 start]; then read the printed result: operation = <NSThread: 0x1700706c0> {number = 1, name = main} operation2 = <NSThread: 0x170075a40> {number = 4, name = (null )}
NSOperationQueue:
NSOperationQueue for running: NSOperation you can call the restart start retry method to execute the running task, but the default method is synchronous.
If you add NSOperation to NSOperationQueue (Operation Queue), the system will automatically perform operations in NSOperation asynchronously.
Add the operation to NSOperationQueue, perform the operation automatically, and enable the thread automatically.
Example:
-(Void) viewDidLoad {[super viewDidLoad]; NSInvocationOperation * operation1 = [[NSInvocationOperation alloc] initWithTarget: self selector: @ selector (thread1) object: nil]; NSInvocationOperation * operation2 = [[NSInvocationOperation alloc] initWithTarget: self selector: @ selector (thread2) object: nil]; NSBlockOperation * operation3 = [NSBlockOperation blockOperationWithBlock: ^ {NSLog (@ "currentThread3 = % @", [NSThread currentThread]);}]; [operation3 addExecutionBlock: ^ {NSLog (@ "currentThread3_1 = % @", [NSThread currentThread]);}]; NSOperationQueue * queue = [[NSOperationQueue alloc] init]; // Add the operation to the queue // method 1 [queue addOperation: operation1]; [queue addOperation: operation2]; [queue addOperation: operation3]; // method 2 [queue addOperationWithBlock: ^ {NSLog (@ "currentThread4 = % @", [NSThread currentThread]);}];}-(void) thread1 {NSLog (@ "currentThread1 = % @", [NSThread currentThread]);}-(void) thread2 {NSLog (@ "currentThread2 = % @", [NSThread currentThread]);}
View results:
currentThread1 = <NSThread: 0x17006f440>{number = 4, name = (null)}currentThread2 = <NSThread: 0x17007d9c0>{number = 5, name = (null)}currentThread3_1 = <NSThread: 0x17007d9c0>{number = 5, name = (null)}currentThread3 = <NSThread: 0x17006f440>{number = 4, name = (null)}currentThread4 = <NSThread: 0x17007d9c0>{number = 5, name = (null)}