struct Test {
var body: some View {
AsyncView(
operation: { try await getData("https://localhost") },
content: { json
Text("data retrieved")
}
)
}
func getData(_ url: String) async throws -> String {
return try await getRequest(url)
}
}
static func getRequest(_ sourceData: String?, _ useWindowsChar1252: Bool = false) async throws -> String {
guard let sourceData else {
return ""
}
var request = URLRequest(url: URL(string: sourceData.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)!)!)
request.httpMethod = "get"
let (data, _) = try await URLSession(configuration: .ephemeral).data(for: request)
return useWindowsChar1252 ?
String(data: data, encoding: .windowsCP1252)! :
String(decoding: data, as: UTF8.self)
}
You can just have one tab item in the TabView and the http request keeps on looping. any idea?
struct MyTabView: View {
var body: some View {
TabView {
Test()
.tabItem {
Label("test", systemImage: "leaf")
}
}
}
}
Trying to understand the cause. Because If I put the AsyncView, say inside a ZStack, it works ok.
Thoughts?