Ios multi-thread operation-GCD scheduling Group

Source: Internet
Author: User

Ios multi-thread operation-GCD scheduling Group
There is such a need to execute two time-consuming asynchronous operations, and then return to the main thread to execute the operation after the two asynchronous operations are completed. If you want to achieve this quickly and efficiently, you can use a scheduling group. The scheduling Group Creation code is as follows:

dispatch_group_t group = dispatch_group_create();
The function for adding a task to a scheduling group is as follows:
dispatch_group_async(dispatch_group_t group, dispatch_queue_t queue, ^(void)block)
The first parameter group is a scheduling group, and the queue is an asynchronous queue. It doesn't make much sense to use the synchronous queue, but it still achieves the effect. The third parameter is an Execution code block, tasks are added to the code block. The following code adds a task to a scheduling group and receives a notification:
// Group-monitor a group of tasks in a unified manner: dispatch_group_t group = dispatch_group_create (); dispatch_queue_t q = dispatch_get_global_queue (0, 0); // Add a task // group to monitor tasks, queue is responsible for scheduling the task dispatch_group_async (group, q, ^ {[NSThread sleepForTimeInterval: 1.0]; NSLog (@ "task 1% @", [NSThread currentThread]);}); dispatch_group_async (group, q, ^ {NSLog (@ "task 2% @", [NSThread currentThread]) ;}); dispatch_group_async (group, q, ^ {NSLog (@ "task 3% @ ", [NSThread currentThread]) ;}); // listen to all tasks completed-after all tasks in the group are executed," tasks in the queue scheduling block are executed asynchronously! "Dispatch_group_notify (group, dispatch_get_main_queue (), ^ {// modify the main queue and batch download in the background. After the end, the main thread updates the UI NSLog (@" OK % @", [NSThread currentThread]) ;}); NSLog (@ "come here ");


The code execution result is as follows:
The download sequence of the task and the appearance of "come here" are not sure, but the update operation of the main thread will always be executed at the end!
The GCD scheduling group also has a function for adding tasks: dispatch_group_enter (dispatch_group_t group) dispatch_group_leave (dispatch_group_t group). The two functions need to be paired and appear, for example:
// Group-monitor a group of tasks in a unified manner: dispatch_group_t group = dispatch_group_create (); dispatch_queue_t q = dispatch_get_global_queue (0, 0 ); // 1> the block after the group is added will be listened to by the group. // dispatch_group_enter must be paired with dispatch_group_group_group; dispatch_async (q, ^ {NSLog (@ "task1% @", [NSThread currentThread]); // At the end of the block, add a dispatch_group_leave (group) After all tasks are executed );}); // re-join dispatch_group_enter (group); dispatch_async (q, ^ {[NSThread sleepForTimeInterval: 1.0]; NSLog (@ "task2% @", [NSThread currentThread]); // At the end of the block, add a dispatch_group_leave (group) ;}) after all tasks are executed; // The group ends dispatch_group_policy (group, dispatch_get_main_queue (), ^ {NSLog (@ "OVER") ;}); NSLog (@ "come here ");


The effect of code execution is the same as that of code execution! There is also a function in GCD.
dispatch_group_wait(dispatch_group_t group, dispatch_time_t timeout)
This function is a blocking wait. When we set the second parameter to DISPATCH_TIME_FOREVER, the group task will not be executed, and subsequent code will not be executed, that is to say, as long as the task does not run the "come here" command, it will not print.

Related Article

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.