Get immediate access to this and over 1,500+ other videos and books.

Boost your skills with a raywenderlich.com Beginner subscription. With over 60+ video courses and our core foundational programming books bundled in one subscription, it’s simply the best investment you can make in your development career.

Challenge: Run Code on the Main Thread

In this challenge episode you will put your existing knowledge about Swift concurrency to the test in order to ensure some code runs on the main thread.

Contributors

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

Welcome to the fourth episode! We haven’t talked about URLSession yet, but don’t worry, that is coming up in the next video.

await MainActor.run {
  print("Found the title. Are we on the main thread? \(Thread.isMainThread)")
  print(line)
}
@MainActor func printTitleAnnotated(url: URL) async throws {
  for try await line in url.lines {
    if line.contains("<title>") {
      print("Found the title. Are we on the main thread? \(Thread.isMainThread)")
      print(line)
      
      return
    }
  }
}
Task {
  print("Starting task. Are we on the main thread? \(Thread.isMainThread)")
  
  try await printTitleAnnotated(url: URL(string: "https://www.raywenderlich.com")!)
}

Reviews

Comments