There are three main methods for iOS Multithreading
(1) nsthread
(2) nsoperation
(3 )**
The following describes the three methods.
1. nsthread
The call method is as follows:
If the function needs to input parameters, it can be passed in from the object. (1) [nsthread detachnewthreadselector: @ selector (threadinmainmethod :) totarget: Self withobject: Nil];
(2) nsthread * mythread = [[nsthread alloc] initwithtarget: Self selector: @ selector (threadinmainmethod :) object: Nil]; [mythread start]; (3) [OBJ ready mselectorinbackground: @ selector (threadme) withobject: Nil];
Question: If a viewcontroller runs a thread and the viewcontroller is release before the thread ends, what will happen?
After testing, the thread does not end, the viewcontroller keeps it, And the dealloc method is not executed.
2. nsoperation
Nsoperation is also a type of multithreading, and nsopertaion has two forms
(1) Concurrent execution For concurrent execution, you need to reload the following four methods // to execute the main function of the task, the entry function of the thread running-(void) Start // whether concurrent execution is allowed, yes is returned, and concurrency is allowed, no is returned. By default, no-(bool) isconcurrent-(bool) isexecuting // indicates whether it has been completed. This must be reloaded. Otherwise, nsopertaion placed in nsoperationqueue cannot be released normally. -(Bool) isfinished, such as testnsoperation: nsoperaion, reload the preceding four methods and declare an nsoperationqueue. Required * queue = [[[nsoperationqueue alloc] init] autorelease: testnsoperation];
It will automatically call the start function in testnsoperation. If you need multiple nsoperation, you need to set some attributes of the queue. If multiple nsoperation dependencies exist, you can also set them, for more information, see the API documentation.
(2) Non-concurrent execution -(Void) Main only needs to reload this main method.
3 .**
** It is very powerful and has little experience. But scorpiozj summary is more comprehensive (http://www.cnblogs.com/scorpiozj/archive/2011/07/25/2116459.html)
At the same time, this articleArticleMore detailed http://www.cnblogs.com/vinceoniphone/archive/2011/04/07/2007968.html also introduced
Official tutorial
** It is closely related to the block, so it is best to first understand the block (you can view it here ). ** it is a C-level function, which means it also provides the C function pointer as a parameter to facilitate the CProgramMember.
Next, let's take a look at ** usage:
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 ).
Multi-line is used in the programCheng is becauseThe 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 for processing.
1. Main queue:
As the name suggests, it runs in the main thread and is obtained by dispatch_get_main_queue.The main queue must be used for UI-related tasks..
2. sErial quque (private dispatch Queue)
EachRun a task. You canAdd multiple instances and execute the FIFO sequence. Generally, these instances are generated by programmers, 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.
Therefore, we can get a general idea of using the ** framework:
Dispatch_async (getdataqueue,^{
//Obtain data. After obtaining a group, refresh the UI.
Dispatch_aysnc (mainqueue,^{
//Ui updates must be performed in the main thread.
};
}
)
It can be seen that ** is very simple to use. In my experience, we will gradually use nsoperation instead **.