I have a Loading view saying "Please wait" that I want to be presented until the data from the API comes back. However, self.showLoading = false is executed even before store.getHomeItems() is finished.
How can I do it ?
struct HomeView: View {
@State var showLoading = false
...
if (self.showLoading == true) {
LoadingView()
} else {
//my content
}
...
ScrollView {
//my scroll content
}
.onAppear {
self.showLoading = true
DispatchQueue.main.async {
store.getHomeItems()
}
self.showloading = false
...
}
I also dont know if I can call Dispatch on my HomeView, since my API layer has also an another DispatchQueue.
URLSession.shared.dataTask(with: url) { (data, _, _) in
guard let data = data else { return }
let myItems = try! JSONDecoder().decode([ItemModel].self, from: data)
DispatchQueue.main.asyncAfter(deadline: .now() + 10) {
completion(myItems)
}
}
.resume()
Thank you