Skip to content

Instantly share code, notes, and snippets.

@benigumocom
Last active March 26, 2024 13:38
Show Gist options
  • Select an option

  • Save benigumocom/ee3dd8a37f752847fa65c4842491b315 to your computer and use it in GitHub Desktop.

Select an option

Save benigumocom/ee3dd8a37f752847fa65c4842491b315 to your computer and use it in GitHub Desktop.
【SwiftUI】task の id を使って処理を何度も繰り返す 👉 https://android.benigumo.com/20240326/task-id/
import SwiftUI
struct TestTaskID: View {
@State private var text = ""
@State private var taskID = UUID()
var body: some View {
Text("\(text)")
.task(id: taskID) { // *
let url = URL(string: "https://worldtimeapi.org/api/timezone/Asia/Tokyo.txt")!
do {
var lines: [String] = []
for try await line in url.lines {
lines.append(line)
}
text = String(lines[2].components(separatedBy: "T")[1])
} catch {
text = "Faild to load."
}
}
Button("RELOAD") {
taskID = UUID()
}
.buttonStyle(.borderedProminent)
}
}
#Preview {
TestTaskID()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment