1

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.

2 Answers2

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