Svbtle

 

Svbtle

GCD: Using Dispatch Groups for Fun and Profit

I’m republishing this article here so I can reference it later.

I recently needed a way to perform an unknown number of asynchronous http requests and wait until they were all done before proceeding. dispatch_groups are a neat feature of Grand Central Dispatch (GCD) that made this easy to do.

There are a couple of ways to use dispatch_groups but the basic idea is the same: create a dispatch_group, give it some tasks, and wait for it to finish those tasks.

First, let’s create a group:

dispatch_group_t group = dispatch_group_create(); 

There are two ways to add tasks to our dispatch_group. You can either call dispatch_group_async with the group, a dispatch_queue and block to run or manually call dispatch_group_enter.

Calling dispatch_group_async looks something like this:

...

Continue reading