Reputation: 454
I need to refresh ContentView after timeout on one of the child of a child. For some reason this code does not work:
struct ContentView: View {
@State private var returnToHome: Bool = false
var body: some View {
TabView([
TabBarItem(view: HomeView(),
image: Image("home_icon"),
title: "home"),
TabBarItem(view: AskView(returnToHome: self.$returnToHome),
image: Image("ask_icon"),
title: "ask us"),
TabBarItem(view: JobsView(),
image: Image("jobs_icon"),
title: "jobs"),
TabBarItem(view: MeetingView(),
image: Image("meeting_icon"),
title: "meeting"),
TabBarItem(view: MoreView(),
image: Image("more_icon"),
title: "more")
])
}
}
struct AskView: View {
@Binding var returnToHome: Bool
var body: some View {
AskDone(returnToHome: self.$returnToHome)
}
}
struct AskDone: View {
@Binding var returnToHome: Bool
var body: some View {
Text("done!")
}
.onAppear {
DispatchQueue.main.asyncAfter(deadline: .now() + 2.0) {
self.returnToHome = true
}
}
}
}
Code inside DispatchQueue is performed but it does not affect ContentView @State. I've also tried to pass Binding but without success. What can be the problem?
Upvotes: 1
Views: 134
Reputation: 258345
in my understanding "body" should be updated anytime when any State of of the View is changed. Am I wrong?
Theoretically yes, but it is declared officially that SwiftUI rendering engine optimized to do not any unneeded re-build/re-draw if nothing changed in view hierarchy, and this is very good, actually. In your case nothing changed, so nothing updated. You have to add some dependency in body
on returnToHome
.
Upvotes: 1