0
  • I make three async requests together all of which I need to wait for before moving forward.

  • I have read following to synchronize async requests

Waiting until two async blocks are executed before starting another block

dispatch_group_async(group, queue, ^ {
...
});

runs task async. But my task itself is async and it calls callback handlers.

So, when will my task be declared done in the context of dispatch_group_async?

Would it be called done when my callbacks have been executed?

If you could provide sequence of events that will happen till the point my task will be declared done will be very helpful

Community
  • 1
  • 1
GJain
  • 5,025
  • 6
  • 48
  • 82
  • What do you mean by "my task" - The async tasks in the dispatch_group? The task that is dispatching the tasks on the dispatch_group? – Paulw11 Mar 03 '15 at 05:50
  • The block that will be executed in dispatch_group_async will have async n/w requests – GJain Mar 03 '15 at 05:52
  • So once the block queues the async network requests that task will then continue to release the dispatch_group, so from the point of view of the dispatch_group (and the execution of a `dispatch_group_notify` or `dispatch_group_wait`) the tasks will be complete even though the network activity is still continuing in the background. – Paulw11 Mar 03 '15 at 05:55
  • Yes, that would be one way of meeting your requirements – Paulw11 Mar 03 '15 at 05:57

1 Answers1

1

If you dispatch an asynchronous task, say "task B", inside a dispatch_group_async, say "Task A" then Task A will be considered complete as soon as the end of the Task A block is reached even though Task B may still be executing.

So, given

dispatch_async_group(group,queue, ^{ 
      [someAsyncTask B];
});

dispatch_async_group(group,queue, ^{ 
      [someAsyncTask C];
});

dispatch_async_group(group,queue, ^{ 
      [someAsyncTask D];
});

dispatch_group_wait(group,10000);

The dispatch_group_wait will return as soon as the final dispatch_async_group block finishes executing even though tasks B/C/D may still be running.

If you use synchronous dispatch for tasks B/C/D then your dispatch group will be considered complete after B,C & D finish.

Paulw11
  • 108,386
  • 14
  • 159
  • 186