I want to call two apis simultaneously. And the case is, when i got successful response of both apis, then only third api should be called. Here i won't use any flag or variable to check if both apis are successful. Please suggest if there is any way to do that.
Asked
Active
Viewed 90 times
1
-
Why won't you use a flag? – stevenpcurtis May 04 '18 at 07:55
-
Because It is not proper way to do that. Any variable shall not be used for just checking both api responses came or not. – Dhaval Trivedi May 04 '18 at 13:05
2 Answers
1
You can use DispatchGroup
let group = DispatchGroup()
group.enter()
perform(request: first) {
group.leave()
}
group.enter()
perform(request: second) {
group.leave()
}
group.notify(queue: .main) {
print("both done")
}

Ivan Smetanin
- 1,999
- 2
- 21
- 28
1
you can use Dispatch group to create a group of tasks
here example
// First, we create a group to synchronize our tasks
let group = DispatchGroup()
// The 'enter' method increments the group's task count…
group.enter()
ApiRequest1.Data { data in
// …while the 'leave' methods decrements it
group.leave()
}
group.enter
ApiRequest2.Data { data in
group.leave()
}
// This closure will be called when the group's task count reaches 0
group.notify(queue: .main) { [weak self] in
}

Abdelahad Darwish
- 5,969
- 1
- 17
- 35