Satish A Satish A - 5 months ago 15
iOS Question

How to handle this: There are five api requests running parallelly and 2nd request is dependent on 4th request's response

I've came across a question and I want to know better approach to that.

There are five api requests running parallelly and 2nd request is dependent on 4th request's response but all 5 requests are already running. What would be the better approach?

Need suggestions. Thanks in advance.

Answer

dispatch ground works fine.

//create a dispatch group
dispatch_group_t serviceGroup = dispatch_group_create();

//request 1
dispatch_group_async(serviceGroup,queue,^{
    // some work here
});

//request 3
dispatch_group_async(serviceGroup,queue,^{
    // some work here
});

//request 5
dispatch_group_async(serviceGroup,queue,^{
    // some work here
});

//request 4
dispatch_group_enter(serviceGroup);
[something startWithCompletion:^() {
    //result
    dispatch_group_leave(serviceGroup);
    //request 2
}];