Skip to content

Instantly share code, notes, and snippets.

@arslanbilal
Last active August 21, 2023 21:55
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save arslanbilal/5bf493226eb6a3691a57915689e303dc to your computer and use it in GitHub Desktop.
Save arslanbilal/5bf493226eb6a3691a57915689e303dc to your computer and use it in GitHub Desktop.
Multiple Request with Dispatch Group
let firstRequestGroup = DispatchGroup()
let secondRequestGroup = DispatchGroup()
for () {
requestGroup.enter()
firstRequest() {
print("DEBUG: FIRST Request")
if success {
requestGroup.enter()
secondRequest() {
print("DEBUG: SECOND Request")
requestGroup.leave()
}
}
requestGroup.leave()
}
}
// This only gets executed once all the requests in the authRequestGroup are done (i.e. FIRST, SECOND requests)
requestGroup.notify(queue: DispatchQueue.main, execute: {
secondRequestGroup.enter()
aRequest() {
print("DEBUG: THIRD Request")
secondRequestGroup.leave()
}
}
// Note: Any code placed here will be executed before the THIRD request completes! To execute code after the THIRD request, we need the request secondRequestGroup.notify like below
print("This gets executed before the THIRD request completes")
// This only gets executed once all the requests in the secondRequestGroup are done (i.e. THIRD request)
secondRequestGroup.notify(queue: DispatchQueue.main, execute: {
// Here, you can update the UI, HUD and turn off the network activity indicator
print("DEBUG: all Done")
}
import Alamofire
struct SequentialRequest {
static func fetchData() {
let authRequestGroup = DispatchGroup()
let requestGroup = DispatchGroup()
var results = [String: String]()
//First request - this would be the authentication request
authRequestGroup.enter()
Alamofire.request("http://httpbin.org/get").responseData { response in
print("DEBUG: FIRST Request")
results["FIRST"] = response.result.description
if response.result.isSuccess { //Authentication successful, you may use your own tests to confirm that authentication was successful
authRequestGroup.enter() //request for data behind authentication
Alamofire.request("http://httpbin.org/get").responseData { response in
print("DEBUG: SECOND Request")
results["SECOND"] = response.result.description
authRequestGroup.leave()
}
authRequestGroup.enter() //request for data behind authentication
Alamofire.request("http://httpbin.org/get").responseData { response in
print("DEBUG: THIRD Request")
results["THIRD"] = response.result.description
authRequestGroup.leave()
}
}
authRequestGroup.leave()
}
// This only gets executed once all the requests in the authRequestGroup are done (i.e. FIRST, SECOND AND THIRD requests)
authRequestGroup.notify(queue: DispatchQueue.main, execute: {
// Here you can perform additional request that depends on data fetched from the FIRST, SECOND or THIRD requests
requestGroup.enter()
Alamofire.request("http://httpbin.org/get").responseData { response in
print("DEBUG: FOURTH Request")
results["FOURTH"] = response.result.description
requestGroup.leave()
}
//Note: Any code placed here will be executed before the FORTH request completes! To execute code after the FOURTH request, we need the request requestGroup.notify like below
print("This gets executed before the FOURTH request completes")
//This only gets executed once all the requests in the requestGroup are done (i.e. FORTH request)
requestGroup.notify(queue: DispatchQueue.main, execute: {
//Here, you can update the UI, HUD and turn off the network activity indicator
for (request, result) in results {
print("\(request): \(result)")
}
print("DEBUG: all Done")
})
})
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment