Beginning Networking with URLSession

Sep 13 2022 · Swift 5.6, iOS 15, Xcode 13.4.1

Part 2: Download Data

15. Grouping Requests

Episode complete

Play next episode

Next
About this episode

Leave a rating/review

See forum comments
Cinema mode Mark complete Download course materials
Previous episode: 14. Show Download Progress Next episode: 16. Simulate Different Network Speeds

Get immediate access to this and 4,000+ other videos and books.

Take your career further with a Kodeco Personal Plan. With unlimited access to over 40+ books and 4,000+ professional videos in a single subscription, it's simply the best investment you can make in your development career.

Learn more Already a subscriber? Sign in.

Notes: 15. Grouping Requests

URLSession - Apple Developer

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

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()