im trying to fetch some data from a Http Api and use the responded JSON to update the text in my SwiftUI view accordingly
The problem is, that I need to fetch the data two times per second and I don't really know how to refresh my view accordingly.
This is how I fetch the data
var timer = Timer()
func scheduleTimer()
timer = Timer.scheduledTimer(timeInterval: 0.2, target: self, selector: #selector(fetchData), userInfo: nil, repeats: true)
}
@objc func fetchData() {
let url:URL = URL(string: "http://XXX.XXX.XXX.23:8085/telemachus/datalink?altitude=v.altitude&longitude=v.long&latitude=v.lat&name=v.name")!
URLSession.shared.dataTask(with: url) { (data, response, error) in
if let error = error { print(error) }
guard let data = data else {return}
do {
let jsonData = try JSONDecoder().decode(JsonData.self, from: data)
let altitude = jsonData.altitude
print(altitude)
} catch let error {
print(error)
}
}.resume()
}
I used a timer to repeat this action. I also have a Model for the JsonData. @EnvironmentObject is not usable in this case because the function is not inside the view.