In my case, when an App launches, I want to call 3 web APIs using NSURLSessionDataTask
. I have 3 different methods for each API and inside each method, there is one NSURLSessionDataTask
.
I want to shape my code in such a way that these 3 methods execute serially. Because, method B is depended on method A's response and method C is depended on method B's response. Each method also doing some database operation after getting API response. So, I need serial execution of methodA
, methodB
and methodC
.
I know this is a logical thing but I want to use dispatch_semaphore_t
or dispatch_group_wait
but I have absolutely no clue on how I can use those in a conjuction with NSURLSessionDataTask
.
I have tried with this:
dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
[self syncA:^(BOOL success) {
NSLog(@"syncA — Completed");
dispatch_semaphore_signal(semaphore);
}];
dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
[self syncB:^(BOOL success) {
NSLog(@"syncB — Completed");
dispatch_semaphore_signal(semaphore);
}];
dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
[self syncC:^(BOOL success) {
NSLog(@"syncC — Completed");
dispatch_semaphore_signal(semaphore);
}];
dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
NSLog(@"************* END OF SYNC *************");
The above code is stuck on first method.