I have been stuck by this wired problem for a couple of days. The code is very simple just as below:
struct PlaygroundView: View{
@ObservedObject var obs:OBSObject = OBSObject()
@Binding var bindingView:BindingView
init(bindingView:Binding<BindingView>){
self._bindingView = bindingView
print("playground inits")
}
var body: some View {
Button(action: Click2){
Text("Click here")
}
}
func Click1(){
obs.theOnlyProperty.toggle()
}
func Click2(){
bindingView.obs.theOnlyProperty.toggle()
}
}
//OBSObject
class OBSObject : ObservableObject{
@Published var theOnlyProperty:Bool = false
}
//BindingView
struct BindingView : View{
@ObservedObject var obs : OBSObject = OBSObject()
var body: some View {
PlaygroundView(bindingView: Binding<BindingView>(
get: { self },
set: { _ in }
))
}
}
//the entry of the app
@main
struct ReinitApp: App {
var body: some Scene {
WindowGroup {
BindingView()
}
}
}
The wired thing is that if I trigger Click1 func and toggle "theOnlyProperty" which is a bool published property in obs object, it will not cause reinitialize the PlaygroundView. However, if I trigger Click2 func, the PlaygroundView view will be reinitialized. The only difference is in Click1, the obs object is belong to the current view whereas in Click2,the obs object is belong to another view. My question is, is there any way to update obs object in the bindingView without causing the current view PlaygroundView reinitializes itself? Any help would be much appreciated!