Multithreading in iOS development

Source: Internet
Author: User

Multi-thread is used when parallel operations are required in the program.

1,

[Self defined mselectorinbackground: @ selector (task) withobject: Nil];

2. nsthread indicates the thread to be executed. You can use this class to encapsulate thread operations. You can use this class to create and manage threads.

A. Use the class method detachnewthreadselector: totarget: withobject: Create a New thread.

[Nsthread detachnewthreadselector: @ selector (task) totarget: Self withobject: Nil];

B. Create an nstheard object.

Nsthread * thr = [[nsthread alloc] initwithtarget: Self selector: @ selector (task) object: Nil];

[Thr start];

C. Custom thread object @ interface mythread: nsthread @ implementation mythread-(void) Main {// Add thread method here} mythread * _ thread = [[mythread alloc] init];

[Mythread start];

3. nsoperationqueue: As the operation manager, operationqueue is responsible for executing all the operation objects in it.

-(Ibaction) caculate :( ID) sender

{

Nsoperationqueue * queue = [[nsoperationqueue alloc] init];

[Queue setmaxconcurrentoperationcount: 1]; // specifies the number of operations that can be concurrently executed

Nsinvocationoperation * OP1 = [[nsinvocationoperation alloc] initwithtarget: Self selector: @ selector (xiazai1) object: Nil];

Nsinvocationoperation * OP2 = [[nsinvocationoperation alloc] initwithtarget: Self selector: @ selector (xiazai2) object: Nil];

Nsinvocationoperation * OP3 = [[nsinvocationoperation alloc] initwithtarget: Self selector: @ selector (xiazai3) object: Nil];

[Queue addoperation: OP1]; // Add the created operation to the running queue

[Queue addoperation: OP2];

[Queue addoperation: OP3];

}

// Download images from the Internet

-(Void) xiazai1

{

Nsdata * data1 = [nsdata datawithcontentsofurl: [nsurl urlwithstring: @ "http://www.qqskycn.net/bq/bq_tp/200904/20090404193057343.gif"];

Nslog (@ "data1 = % @", data1 );

Image1.image = [uiimage imagewithdata: data1];

}

-(Void) xiazai2

{

Nsdata * data2 = [nsdata datawithcontentsofurl: [nsurl urlwithstring: @ "http://imgqq.5308.com/20061107/uploadfile/2005/8/11/14511674586.jpg"];

Image2.image = [uiimage imagewithdata: data2];

}

-(Void) xiazai3

{

Nsdata * data3 = [nsdata datawithcontentsofurl: [nsurl urlwithstring: @ "http://img1.comic.zongheng.com/comic/image/2009/1/samosekayi/ori/20090217025931686184.jpg"];

Image3.image = [uiimage imagewithdata: data3];

}

4,

GCD is closely linked to the block, so it is best to first understand the block (you can view it here ). GCD is a C-level function, which means it also provides the C function pointer as a parameter to facilitate C programmers.

The following describes how to use GCD:

dispatch_async(dispatch_queue_t queue, dispatch_block_t block);

Async indicates asynchronous running, block indicates what you want to do, and queue indicates who you want to handle the task. (In addition to async, there are also sync and delay. This article takes async as an example ).

Multithreading is used in the program because the program often needs to read data and then update the UI. for a good user experience, Data Reading operations tend to run in the background, so as to avoid blocking the main thread. there are three types of queue in GCD for processing.

1. Main queue:

As the name suggests, running in the main thread is obtained by dispatch_get_main_queue. Main queue is required for UI-related operations.

2. Serial quque (private dispatch Queue)

You can add multiple tasks each time you run a task. The execution sequence is FIFO, which is usually generated by a programmer. For example:

NSDate *da = [NSDate date];NSString *daStr = [da description];const char *queueName = [daStr UTF8String];dispatch_queue_t myQueue = dispatch_queue_create(queueName, NULL);

3. Concurrent Queue (Global dispatch Queue ):

You can run multiple tasks at the same time. The start time of each task is in the order of adding queue, and the end order depends on the respective tasks. Use dispatch_get_global_queue to obtain the task.

So we can get a general idea about the GCD framework:

Dispatch_async (getdataqueue, ^ {// obtain data. After obtaining a group, refresh UI. dispatch_aysnc (mainqueue, ^ {/UI updates must be performed in the main thread };})

It can be seen that GCD is very simple to use. Based on my experience, we will gradually eliminate the use of nsoperation and switch to GCD.

Finally, Apple lowered the development threshold very low to attract developers. taking multi-threaded programming as an example, there seems to be no easier platform than iOS. this will undoubtedly attract more people to seek gold, but the competition will undoubtedly be fierce. to stand out from the crowd, apps have to be creative. this is a double-edged sword ~~.




Related Article

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.