Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save jimmythai/433036c48af9061088359f8ced1c89b0 to your computer and use it in GitHub Desktop.

Select an option

Save jimmythai/433036c48af9061088359f8ced1c89b0 to your computer and use it in GitHub Desktop.
// Create a dispatch group
let userListDispatchGroup = DispatchGroup()
// Get List of Users
userListDispatchGroup.enter()
Alamofire.request("https://httpbin.org/get?users").validate().responseJSON { response in
switch response.result {
case .success:
print("Get User Successful")
// put user into users array
...
case .failure(let error):
print(error)
}
// leave the dispatch group after the network request is complete and data is parsed
userListDispatchGroup.leave()
}
// Get List of Avatars
userListDispatchGroup.enter()
Alamofire.request("https://httpbin.org/get?avatars").validate().responseJSON { response in
switch response.result {
case .success:
print("Get Avatars Successful")
// put avatar into avatars array
....
case .failure(let error):
print(error)
}
// leave the dispatch group after the network request is complete and data is parsed
userListDispatchGroup.leave()
}
// after both network request complete, code inside this closure will be called
// queue: .main means the main queue, always use main queue to update UI
userListDispatchGroup.notify(queue: .main) {
print("Both get users and get avatars has completed 👌")
self.tableView.reloadData()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment