Created
April 10, 2018 22:41
-
-
Save jimmythai/433036c48af9061088359f8ced1c89b0 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 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