Grand Central Dispatch and concurrent tasks

concurrency, grand-central-dispatch, ios, multithreading

Solution

Use a `dispatch_group`. The Concurrency Programming Guide gives one example, and there is more API that might also help you.

Create a dispatch group using `dispatch_group_create`.

Put each "task" in the group by enqueuing it using `dispatch_group_async`.

(Or, manually tell GCD when each task starts and stops, using `dispatch_group_enter` and `dispatch_group_leave`.)

To run a block when all tasks in the group have completed, enqueue it using `dispatch_group_notify`.

(Or, in the unlikely case that the design of your app allows you to wait synchronously, use `dispatch_group_wait` instead.)

When you're done with the group, `dispatch_release` it.

Problem

I need to perform three tasks that are independent one from each other, so I'd like to execute them concurrently. But I need them all to have finished to notify another object. AFAIK, *dispatch_apply* creates concurrent threads, but it iterates a collection or an array of objects and performs the same task a number of loops, and I want to perform a different task for each thread. Is it possible to do what I want by using GCD? If not, what should be the best way? Thanks!

Original source