Multi-Threading for iOS development

Source: Internet
Author: User

Multithreading:

Create a new thread: it can also be created by Alloc init.

Nsthread *thread1 = [[Nsthread alloc] initwithtarget:self selector: @selector (Taska) Object:nil];

Start thread: [Thread1 start];

Exit Thread: [Nsthread exit];

[Nsthread detachnewthreadselector: @selector (downloadnetworkdata) totarget:self Withobject:nil];

Limitations: The UI thread is called the main thread

Other threads created are called working sub-threads

Note: Do not manipulate the UI directly in a child thread (indirectly let the main thread manipulate the UI)

Create the thread, it's inside the method to execute the main thread, in the main thread of the method to update the UI interface

[Self Performselectoronmainthread: @selector (UpdateUI) Withobject:nil Waituntildone:yes];

(3) Use notification to listen for thread end

[[Nsnotificationcenter Defaultcenter] addobserver:self selector: @selector (dealthreadexit) Name: Nsthreadwillexitnotification Object:nil];

Exit exits.

Use of nsoperation (Operation/action)

(1) What is nsoperation, role

Nsoperation and Nsthread Similar, a mechanism of realizing multithreading

The nsthread made a higher abstraction, joined the block, and was easier to use than the Nsthread

Nsoperation abstract class, using Nsinvocationoperation and Nsblockoperation

(2) Nsinvocationoperation and Nsblockoperation

Nsinvocationoperation *invocationoperation = [[Nsinvocationoperation alloc] initwithtarget:self selector: @selector ( TASK1) Object:nil];

Note: It needs to be executed after creation and is synchronized by default when executed.

[Invocationoperation start];

Nsblockoperation *blockoperation = [Nsblockoperation blockoperationwithblock:^{

for (int i=0; i<20; i++)

{

NSLog (@ "B =%d", i);

}

}];

[Blockoperation start];

Operation Queue, Understanding: Task List

Note: Tasks are executed asynchronously parallel

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

[Queue addoperation:invocationoperation];

[Queue addoperation:blockoperation];

Use of GCD

Grand Central Dispatch Shorthand

Benefits: 1. Support multi-core

2.C and block interface for ease of use

3. Late appearance, powerful, recommended use

Group task groups

dispatch_group_t group = Dispatch_group_create ();

Add Task 7s completion to task group

Dispatch_group_async (Group, Dispatch_get_global_queue (Dispatch_queue_priority_default, 0), ^{

for (int i=0; i<100; i++) {

NSLog (@ "A =%d", i);

[Nsthread sleepfortimeinterval:0.07];

}

});

GCD simplest to open the form of an asynchronous task Dispatch_async (Dispatch_get_global_queue (dispatch_queue_priority_default, 0), ^{

for (int i=0; i<100; i++)

{

UI cannot be updated directly in child threads

Progressview.progress + = 0.01;

Dispatch_async (Dispatch_get_main_queue (), ^{

Progressview.progress + = 0.01;

});

NSLog (@ "progress =%.2f%%", progressview.progress*100);

[Nsthread sleepfortimeinterval:0.1];

}

Final Display dialog box

Dispatch_async (Dispatch_get_main_queue (), ^{

Uialertview *alertview = [[Uialertview alloc] init];

Alertview.message = @ "Download complete";

[Alertview addbuttonwithtitle:@ "Cancel"];

[Alertview show];

});

});

Multi-Threading for iOS development

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.