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