I have an async function with a block :
[self performAsyncTaskCompletion:(void(^) () )
{
//Do Something
}
];
I need to call this function many times but in a synchronous way. I tried to use GCD queues :
dispatch_queue_t queue = dispatch_queue_create("com.MyApp.task", NULL);
for (int i = 0; i < array.count; i++)
{
dispatch_sync(queue, ^{
[self performAsyncTaskCompletion:(void(^) () )
{
//Do Something
}
];
});
}
But it doesn't work because dispatch_sync is only waiting for the end of the block. How can I ask it to wait for the end of the async functions in its block ?
You can use
dispatch_async
with semaphores:Example:
Console output:
You could use dispatch group if you want to initiate some process upon the completion of a series of asynchronous tasks, but would like to allow those tasks to run concurrently with respect to each other (which, especially with network requests, can offer much better performance than running them sequentially):
Personally, I'd might even be inclined to perform a more radical refactoring of
performAsyncTaskCompletion
, using an asynchronousNSOperation
subclass pattern instead. Then you could add these to aNSOperationQueue
withmaxConcurrentOperationCount
specified, thereby achieving the same concurrency while also controlling the degree of concurrency. But hopefully the above illustrates the idea: Run tasks concurrently, but detect the completion of those tasks without ever blocking the main thread.