Skip to content

Instantly share code, notes, and snippets.

@lucianoschillagi
Created November 11, 2024 13:02
Show Gist options
  • Save lucianoschillagi/6ba8d5b862bd2a26cdf3473109391035 to your computer and use it in GitHub Desktop.
Save lucianoschillagi/6ba8d5b862bd2a26cdf3473109391035 to your computer and use it in GitHub Desktop.
SwiftUI View Lifecycle methods → task(priority:)
import SwiftUI
struct ContentView: View {
let url = URL(string: "https://dc4p.store")!
@State private var message = "Loading..."
var body: some View {
Text(message)
.task {
do {
var receivedLines = [String]()
for try await line in url.lines {
receivedLines.append(line)
message = "Received \(receivedLines) lines"
}
} catch {
message = "Failed to load"
}
}
.padding()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment