Reputation: 28069
I'm stuck on something which should be trivial, using SwiftUI. I am pulling some data back from my API, and simply want to show each item on my view with ForEach.
I have the following function:
@State var workoutVideoList: GetWorkoutVideoListResponse!
func GetWorkoutVideoList() {
loadingWorkoutVideoList = true
PtPodsApiService.GetWorkoutList(firebaseToken: firebaseAuthToken) { (workoutListResult) -> () in
DispatchQueue.main.async() {
workoutVideoList = workoutListResult
loadingWorkoutVideoList = false
}
}
}
I then want to show another view and pass this object into it:
if workoutVideoList != nil {
BookAppointmentView(workoutVideoList: workoutVideoList)
}
else {
Text("Nope")
}
For whatever reason, my main thread sees workoutVideoList as nil, even though it's been assigned. I thought using the dispatcher to assign it would solve the problem, but no joy!
Any idea what I'm doing wrong here?
Thanks
Upvotes: 0
Views: 1182
Reputation: 29614
Give @State
a default value @State var workoutVideoList: GetWorkoutVideoListResponse = false
and use onAppear
to call GetWorkoutVideoList()
but ideally you will eventually get this working in a ViewModel.
Upvotes: 1