Hi. I have a button that triggers a request to a webservice:
struct ContentView: View {
@StateObject private var state: State = State()
var body: some View {
VStack {
Button("Request", action: { makeRequest() })
Text(state.response)
}
}
private func makeRequest() {
URLSession.shared.dataTask(with: URL(string: "https://my-json-server.typicode.com/typicode/demo/posts/1")!, completionHandler: { data, _, _ in
state.response = String(bytes: data!, encoding: .utf8)!
})
.resume()
}
}
and an ObservableObject to store the response in:
class State: ObservableObject {
@Published var response: String = ""
}
.
It seems to workd, but, I get the error message
[SwiftUI] Publishing changes from background threads is not allowed; make sure to publish values from the main thread (via operators like receive(on:)) on model updates.
So I was wondering if this is the correct way to store the responses of HTTP requests in a StateObject. I also couldn't find anything about receive(on:).
1
0
4.9k