Modern Concurrency: Getting Started

Oct 18 2022 Swift 5.5, iOS 15, Xcode 13.4

Part 2: Asynchronous Sequences

9. Your Second Asynchronous App

Episode complete

Play next episode

Next
Save for later
About this episode
See forum comments
Cinema mode Mark complete Download course materials
Previous episode: 8. Introduction Next episode: 10. Concurrency With async let

This video Your Second Asynchronous App 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.

For this part of the course, you’ll work on this cloud file storage app. Your first job is to download a list of files from the server and display it in this view. It’s very similar to how you downloaded and displayed the list of stock symbols for LittleJohn.

Non-async code in starter

In SuperStorageModel, here’s availableFiles():

func availableFiles() async throws -> [DownloadFile] {
  guard let url = URL(string: "http://localhost:8080/files/list") else {
    throw "Could not create the URL."
  }
  return []

Add async call: async throws / try await

Now, to fetch the data, I added this line, before the return statement:

let (data, response) = try await URLSession.shared.data(from: url)
🟥return []

Handle data, response

Next, to check the server response and return the fetched data, I replaced the dummy return statement:

let (data, response) = try await URLSession.shared.data(from: url)
🟩
guard (response as? HTTPURLResponse)?.statusCode == 200 else {
  throw "The server responded with an error."
}

// Just for a change, I'll throw my own error if `decode` fails...
guard let list = try? JSONDecoder()
  .decode([DownloadFile].self, from: data) else {
  throw "The server response was not recognized."
}
return list

Call async method from SwiftUI view

Next, I updated the SwiftUI view to use this new async method.

// TODO: Call model.availableFiles()
🟩
.task {
  guard files.isEmpty else { return }  // first check if I already fetched the file list
  do {
    files = try await model.availableFiles()  // if not, call `availableFiles()`
  } catch {
    lastErrorMessage = error.localizedDescription
  }
}