Some advanced uses of GCD
注:本小节总结几个GCD的高级用法
1, Dispatch Groups (Dispatch Group)
The Dispatch group notifies you when the entire group's tasks are complete, which can be synchronous or asynchronous, even in different queues. The issue of monitoring the completion of multiple asynchronous tasks. This is undoubtedly a very good choice.
Because the monitored tasks may be in different queues, use one dispatch_group_t instance to write down these different tasks.
The GCD API provides two ways to notify when all events in the group are complete.
The first is dispatch_group_wait that it blocks the current thread until all the tasks in the group are completed or wait for a timeout to occur.
Because you are using synchronous dispatch_group_wait , it blocks the current thread, so you need to dispatch_async put the entire method in the background queue to avoid blocking the main thread.
Dispatch_async (Dispatch_get_global_queue (Dispatch_queue_priority_default,0)) { Let Group= Dispatch_group_create ();//Create a Dispatch_group //dispatch_group_enter manually notifies the dispatch group that the task has started. You have to make sure that Dispatch_group_enter and dispatch_group_leave appear in pairs, or you may encounter a bizarre crash problem. Dispatch_group_enter (Group)//.....Print"SSSs") Dispatch_group_leave (Group) Dispatch_group_wait (Group, Dispatch_time_forever);//have been waitingDispatch_async (Dispatch_get_main_queue (), {//Refresh UI}) }
First create a group, then the code that executes the middle of groupenter and leave. And then use dispatch_group_wait it to keep the thread stuck here waiting. Finally go to the part that refreshes the UI or processes the group results
However, this method of blocking threads appears to be not name friendly. Let's take a look at the second method of not blocking threadsdispatch_group_notify
let groupDown = dispatch_group_create() dispatch_group_enter(groupDown) //.... dispatch_group_leave(groupDown) dispatch_group_notify(groupDown, dispatch_get_main_queue()) { //回调 }
The dispatch_group_notify works in an asynchronous manner. When there is no task in the Dispatch Group, it executes its code
There's another way.
let group1 = dispatch_group_create() 0)) { //并行执行的线程1 } 0)) { //并行执行的线程2 } dispatch_group_notify(group1, dispatch_get_main_queue()) { //回调 汇总结果。这里如果不涉及UI 也可以使用dispatch_get_global_queue }
In general, we're going to use it this way.
2, Dispatch_apply
dispatch_applyBehaves like a For loop, but it can perform different iterations concurrently. This function is synchronous, so just like the normal for loop, it will only return when all the work is done
It is important to be careful when calculating the optimal number of iterations for any given number of jobs within a closure, because too many iterations and only a small amount of work per iteration can cause a lot of overhead so that it offsets any gains from concurrency. And the technology called Striding can help you here, by doing a few more different things in each iteration.
Note the overhead of concurrency.
dispatch_applyNot suitable for serial queues, which is a good choice for concurrent loops, especially if you need to track the progress of a task
Here's a look at the code used
dispatch_apply(3, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)) { (i:Int) ->in print(i) //1,2,0 这里是异步的 每次输出的次序不一定相同 如果每次都会等待网络请求 可以这么搞 }
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Swift 17-----------Some advanced uses of GCD