Get immediate access to this and over 1,500+ other videos and books.

Boost your skills with a raywenderlich.com Beginner subscription. With over 60+ video courses and our core foundational programming books bundled in one subscription, it’s simply the best investment you can make in your development career.

Grouping Requests

Learn how to group multiple URLSession tasks so they execute concurrently, but are reported as completed when all requests have finished.

Contributors

Heads up... You've reached locked video content where the transcript will be shown as obfuscated text.

You can unlock the rest of this video course, and our entire catalogue of books and videos, with a raywenderlich.com Professional subscription.

Welcome back! At times, you may want to group requests together. Right now you download the song and album artwork as two separate requests, each from two different methods.

func download(songAt songURL: URL, artworkAt artworkURL: URL) async throws -> Data {
    
}
typealias Download = (_ url: URL, _ response: URLResponse)
async let song: Download = try session.download(from: songURL)
async let artwork: Download = try session.download(from: artworkURL) 
let (songDownload, artworkDownload) = try await (song, artwork)
guard let songHTTPResponse = songDownload.response as? HTTPURLResponse,
  let artworkHTTPResponse = artworkDownload.response as? HTTPURLResponse,
  songHTTPResponse.statusCode == 200,
  artworkHTTPResponse.statusCode == 200
else {
  throw SongDownloadError.invalidResponse
}
let fileManager = FileManager.default

guard let documentsPath = fileManager.urls(for: .documentDirectory,
                                           in: .userDomainMask).first
else {
  throw SongDownloadError.documentDirectoryError
}
let lastPathComponent = songURL.lastPathComponent
let destinationURL = documentsPath.appendingPathComponent(lastPathComponent)

do {
  if fileManager.fileExists(atPath: destinationURL.path) {
    try fileManager.removeItem(at: destinationURL)
  }
    
  try await fileManager.copyItem(at: song.url, to: destinationURL)
} catch {
  throw SongDownloadError.failedToStoreSong
} 
await MainActor.run {
  downloadLocation = destinationURL
}
do {
  return try Data(contentsOf: artworkDownload.url)
} catch {
  throw ArtworkDownloadError.failedToDownloadArtwork
}
//    .onAppear(perform: {
//      Task {
//        await downloadArtwork()
//      }
//    })
private func downloadSongTapped() async {
    
}
if downloader.downloadLocation == nil {
  guard let artworkURL = URL(string: musicItem.artwork),
    let previewURL = musicItem.previewURL
  else {
    return
  }
} else {
  playMusic = true
}
isDownloading = true
  
defer {
  isDownloading = false
}
do {
  let data = try await downloader.download(songAt: previewURL, artworkAt: artworkURL)
    
  guard let image = UIImage(data: data) else {
    return
  }
    
  artworkImage = image
} catch {
  print(error)
    
  showDownloadFailedAlert = true
}
await downloadSongTapped()

Reviews

Comments