Modern Concurrency: Getting Started

Oct 18 2022 Swift 5.5, iOS 15, Xcode 13.4

Part 2: Asynchronous Sequences

16. Concurrent Downloads

Episode complete

Play next episode

Next
Save for later
About this episode
See forum comments
Cinema mode Mark complete Download course materials
Previous episode: 15. Using Combine Next episode: 17. Conclusion

This video Concurrent Downloads was last updated on Oct 18 2022

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 kodeco.com Professional subscription.

Make sure the course server is running and continue with your project from the previous episode or open the starter project for this episode.

if let offset = offset {
  // Add code for Cloud 9 plan
}
else {
  result = try await URLSession.shared.bytes(from: url)
  guard (result.response as? HTTPURLResponse)?.statusCode == 200 else {
    throw "The server responded with an error."
  }
}
if let offset = offset {
  🟩
  let urlRequest = URLRequest(url: url, offset: offset, length: size)
  🟥
}
extension URLRequest {
  init(url: URL, offset: Int, length: Int) {
    self.init(url: url)
    addValue("bytes=\(offset)-\(offset + length - 1)", forHTTPHeaderField: "Range")
  }
}
0-19999 [offset 0, length 20000]
20000-39999 [offset 20000, length 20000]
40000-59999 [offset 40000, length 20000]
60000-77344 [offset 0, length 17345]
if let offset = offset {
  let urlRequest = URLRequest(url: url, offset: offset, length: size)
  🟩result = try await URLSession.shared.bytes(for: urlRequest)🟥
}
if let offset = offset {
  let urlRequest = URLRequest(url: url, offset: offset, length: size)
  result = try await URLSession.shared.bytes(for: urlRequest)
  🟩
  guard (result.response as? HTTPURLResponse)?.statusCode == 206 else {
    throw "The server responded with an error."
  }
  🟥
}
func multiDownloadWithProgress(file: DownloadFile) async throws -> Data {
  func partInfo(index: Int, of count: Int) -> (offset: Int, size: Int, name: String) {
    let standardPartSize = Int((Double(file.size) / Double(count)).rounded(.up))
    let partOffset = index * standardPartSize
    let partSize = min(standardPartSize, file.size - partOffset)
    let partName = "\(file.name) (part \(index + 1))"
    return (offset: partOffset, size: partSize, name: partName)
  }
  let total = 4
  let parts = (0..<total).map { partInfo(index: $0, of: total) }
  
  // Add code here, replacing placeholder return statement
  return Data()
}
downloadWithProgress(fileName: String, name: String, size: Int, offset: Int? = nil)

Call multiDownloadWithProgress(file:)

Start with part0: Define a promise with async let:

async let part0 =
  downloadWithProgress(fileName: file.name, name: parts[0].name, size: parts[0].size, offset: parts[0].offset)
async let part0 =
downloadWithProgress(fileName: file.name, name: parts[0].name, size: parts[0].size, offset: parts[0].offset)
🟩 
async let part1 =
downloadWithProgress(fileName: file.name, name: parts[1].name, size: parts[1].size, offset: parts[1].offset)
async let part2 =
downloadWithProgress(fileName: file.name, name: parts[2].name, size: parts[2].size, offset: parts[2].offset)
async let part3 =
downloadWithProgress(fileName: file.name, name: parts[3].name, size: parts[3].size, offset: parts[3].offset)
🟥 
try await [part0, part1, part2, part3]
try await [part0, part1, part2, part3]
  🟩.reduce(Data(), +)🟥
🟩return 🟥try await [part0, part1, part2, part3]
  .reduce(Data(), +)
❌return Data()
isDownloadActive = true
Task {  // delete downloadTask =
  do {
    fileData = try await model.🟩multiDownloadWithProgress🟥(file: file) 
  } catch { }
  isDownloadActive = false
  // delete timerTask...
}