1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
| struct TaskView: View { var body: some View { Circle() .task { await fetch(title: "from .task: ") } .onAppear { print("onAppear") Task { await fetch(title: "from onAppear::Task: ") } } .onDisappear { print("onDisappear") } }
func fetch(title: String) async { print("\(title) Started")
let request = URLRequest(url: URL(string: "https://hmhv.info")!, cachePolicy: .reloadIgnoringLocalAndRemoteCacheData) do { let (_, response) = try await URLSession.shared.data(for: request) print("\(title) \((response as! HTTPURLResponse).statusCode)") } catch { print("\(title) Error : \(error.localizedDescription)") }
print("\(title) Finished") } }
|