Kwaadpepper Kwaadpepper - 2 months ago 14
iOS Question

IOS recursive dispatch async

I'm using xcode6 with ARC.

I have a login storyboard with login stuff.
I have a login process wich a wish to run in background (other thread).
So cool 'im using :

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{
// Call Async URL
});


But i have some difficulties mostly because I'have to call multiple async URLs.

So no problem, I try to implement this:
enter image description here

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{
dispatch_semaphore_t sema = dispatch_semaphore_create(0);
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{
// Call Async URL withcallback block
// In block
dispatch_semaphore_signal(sema);
});
// Wait 30 seconds max to finish
dispatch_semaphore_wait(sema, dispatch_time(DISPATCH_TIME_NOW, 30*10^9));

sema = dispatch_semaphore_create(0);
// check if is ok to continue

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{
// Call Async URL withcallback block
// In block
dispatch_semaphore_signal(sema);
});
// Wait 30 seconds max to finish
dispatch_semaphore_wait(sema, dispatch_time(DISPATCH_TIME_NOW, 30*10^9));
});


Is it ok to do this or is there an other way ?

EDIT: I have to do much more that 2 calls and it would be a mess to recursive all my asynch call.

Answer Source

I'm not sure you need to complicate things this much. You are dispatch_async to the same queue and you indicate that the calls you're making are themselves asynchronous.

Assuming that // Call Async URL withcallback block indicates the call you're making is also asynchronous and takes a callback block that it executes upon completion, all of your source code above could be replaced with the following:

[<some async call> block:^{
  [<some other async call> block:^{
  }];
}];