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
About this episode

Leave a rating/review

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

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.

Heads up... You’re accessing parts of this content for free, with some sections shown as obfuscated text.

Heads up... You’re accessing parts of this content for free, with some sections shown as obfuscated text.

Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.

Unlock now

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...
}