Created
February 7, 2019 08:09
-
-
Save dimebt/feb0766eb7c807967656106949c4de04 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
| private func downloadPhotos() { | |
| // 1. Show preloading animation | |
| self.activityIndicator.startAnimating() | |
| // 2. Fetch and serialize json response | |
| Alamofire.request(photosURL, method: .get).validate().responseData { (data) in | |
| guard let data = data.result.value else { return } | |
| guard let photos = try? JSONDecoder().decode([Photo].self, from: data) else { return } | |
| // 3. Download photos | |
| for photo in photos { | |
| guard let photoUrl = URL(string: photo.url) else { return } | |
| guard let photoData = try? Data(contentsOf: photoUrl) else { return } | |
| guard let photoImage = UIImage(data: photoData) else { return } | |
| self.photos.append(photoImage) | |
| } | |
| // 4. Hide preloading animation and update the UI | |
| self.activityIndicator.stopAnimating() | |
| self.photoCollectionView.reloadData() | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment