Beginning Networking with URLSession

Sep 13 2022 · Swift 5.6, iOS 15, Xcode 13.4.1

Part 1: Introduction to URLSession & Concurrency

04. Challenge: Run Code on the Main Thread

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: 03. More Modern Concurrency Next episode: 05. Session Configurations

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.

Notes: 04. Challenge: Run Code on the Main Thread

Concurrency - The Swift Programming Language

Heads up... You've reached locked video content where the transcript will be shown as obfuscated text.

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")!)
}