Skip to content

Instantly share code, notes, and snippets.

@dimebt
Created February 7, 2019 08:09
Show Gist options
  • Select an option

  • Save dimebt/feb0766eb7c807967656106949c4de04 to your computer and use it in GitHub Desktop.

Select an option

Save dimebt/feb0766eb7c807967656106949c4de04 to your computer and use it in GitHub Desktop.
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