Modern Concurrency: Getting Started

Oct 18 2022 · Swift 5.5, iOS 15, Xcode 13.4

Part 2: Asynchronous Sequences

09. Your Second Asynchronous App

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: 08. Introduction Next episode: 10. Concurrency With async let

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

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