GCD multi-threaded tips in iOS development
We often use:
dispatch_async(dispatch_get_global_queue(0, 0), ^{ // 处理耗时操作的代码块 //通知主线程刷新 dispatch_async(dispatch_get_main_queue(), ^{ //回调或者说是通知主线程刷新 }); });
Where Main_queue is the default serial queue for the system, Global_queue is the default parallel queue for the system.
What is a serial queue (Serial)?
Create any number of serial queues, each of which performs the added task sequentially, one queue at a time can perform only one task (serial), but the individual queues do not affect each other and can be executed concurrently. The tasks in each queue run on a separate thread maintained by their respective serial queue, with only one thread in one queue.
What is a parallel queue (Concurrent)?
Parallel queues are not allowed to be created by themselves, and there are three parallel queues in the system with different priority levels. Parallel queues still start tasks in the order in which they are added, but the latter task does not have to wait for the previous task to complete, but instead starts the next task immediately after starting the first one. There is a system decision as to how many tasks are allowed to run concurrently at the same time. Each task runs on a separate thread that the parallel queue provides for them, and how many threads must be maintained at the same time in a parallel queue.
There's another concept in GCD called the main dispatch queue.
The main dispatch queue (main dispatch) the task in the main dispatch queue runs on the main thread of the application, so if you want to modify the interface of the application, he is the only option.
As follows:
dispatch_async(dispatch_get_main_queue(), ^{ //跟新界面的操作 });
Execute only once:
static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{
The source of this article just online: http://www.superqq.com/blog/2014/12/27/ioskai-fa-zhong-de-gcdduo-xian-cheng-tips/
GCD multi-threaded tips in iOS development