iOS Development Multithreading One: Understanding the basic use of-nsoperation

Source: Internet
Author: User

#import "ViewController.h"@interfaceViewcontroller ()@end@implementationViewcontroller-(void) Touchesbegan: (Nsset<uitouch *> *) touches withevent: (Uievent *)Event{[self blockoperation];}/** * 1:nsoperation two ways to encapsulate a task: Nsinvocationoperation,nsblockoperation, where two methods do not specify a queue, the default is to perform 2 in the main queue: Home tasks: Addexecutionblock: Note: If the number of tasks in an operation is greater than 1, then the child thread will execute the task concurrently. Note: It is not necessarily a child thread, it is possible*/-(void) invocationopeation{//1. Create operations, encapsulate tasks    /*first parameter: target object Self second parameter: the name of the calling method the third parameter: The parameter that the preceding method needs to accept nil*/nsinvocationoperation*OP1 = [[nsinvocationoperation alloc]initwithtarget:self selector: @selector (DOWNLOAD1)Object: nil]; Nsinvocationoperation*OP2 = [[nsinvocationoperation alloc]initwithtarget:self selector: @selector (download2)Object: nil]; Nsinvocationoperation*OP3 = [[nsinvocationoperation alloc]initwithtarget:self selector: @selector (DOWNLOAD3)Object: nil]; //2. Start | Perform action[OP1 start];     [OP2 start]; [OP3 start];}-(void) blockoperation{//1. Create an actionNsblockoperation *OP1 = [Nsblockoperation blockoperationwithblock:^{NSLog (@"1----%@", [Nsthread CurrentThread]);        }]; Nsblockoperation*OP2 = [Nsblockoperation blockoperationwithblock:^{NSLog (@"2----%@", [Nsthread CurrentThread]);        }]; Nsblockoperation*OP3 = [Nsblockoperation blockoperationwithblock:^{NSLog (@"3----%@", [Nsthread CurrentThread]);        }]; //Append Task//Note: If the number of tasks in an operation is greater than 1, then the child thread will execute the task concurrently//Note: It is not necessarily a child thread, it is possible the main thread[Op3 addexecutionblock:^{NSLog (@"4---%@", [Nsthread CurrentThread]);        }]; [OP3 Addexecutionblock:^{NSLog (@"5---%@", [Nsthread CurrentThread]);        }]; [OP3 Addexecutionblock:^{NSLog (@"6---%@", [Nsthread CurrentThread]);        }]; //2. Start[OP1 start];    [OP2 start]; [OP3 start];}-(void) download1{NSLog (@"%s----%@", __func__,[nsthread CurrentThread]);}-(void) download2{NSLog (@"%s----%@", __func__,[nsthread CurrentThread]);}-(void) download3{NSLog (@"%s----%@", __func__,[nsthread CurrentThread]);}@end

# # #2. nsoperation

-2.1 nsoperation Basic use

(1) Related concepts

Nsoperation is the packaging of GCD.

22 Core Concepts "Queue + operation"

(2) Basic use

The nsoperation itself is an abstract class, only its subclasses

23 subcategories are: Nsblockoperation, nsinvocationoperation, and custom inherited classes from Nsoperation

Nsoperation and Nsoperationqueue in combination for multithreading concurrency

(3) Related code

"' OBJC

Nsinvocationoperation

1. Encapsulation operation

/*

First parameter: target object

Second parameter: The method to be called by the operation, with a maximum of one parameter

The third argument: call the parameter passed by the method, and if the method does not accept the argument, the value is nil

*/

Nsinvocationoperation *operation = [[Nsinvocationoperation alloc]

Initwithtarget:self selector: @selector (run) Object:nil];

2. Start the operation

[Operation start];

-------------------------------------------------

Nsblockoperation

1. Encapsulation operation

/*

Nsblockoperation provides a class method for encapsulating operations in this class of methods

*/

Nsblockoperation *operation = [Nsblockoperation blockoperationwithblock:^{

Executing in the main thread

NSLog (@ "---download1--%@", [Nsthread CurrentThread]);

}];

2. Append operation, append operation executed in child thread

[Operation Addexecutionblock:^{

NSLog (@ "---download2--%@", [Nsthread CurrentThread]);

}];

[Operation Addexecutionblock:^{

NSLog (@ "---download3--%@", [Nsthread CurrentThread]);

}];

3. Start the execution action

[Operation start];

----------------------------------------------

03 Custom Nsoperation

How do I encapsulate an operation?

Custom Nsoperation, by overriding the internal main method to implement encapsulation operations

-(void) Main

{

NSLog (@ "--main--%@", [Nsthread CurrentThread]);

}

How do I use it?

1. Instantiate a custom Action object

Xmgoperation *op = [[Xmgoperation alloc]init];

2. Perform the action

[op start];

```

iOS Development Multithreading One: Understanding the basic use of-nsoperation

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.