UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

SOLVED: @ObservedObject changed, not triggering View Refresh

Forums > SwiftUI

I have been having a problem getting a re-render of a view, when a subview updates observable data. This occurs even though CoreData is used, so I added an additional Observable Object class with just a toggle of a Boolean to trigger the rerender.
Clearly the way I referencing the class is creating a new instance intead of a shared instance of the observable object. Any suggestions to fix?

    @Published var newBid:Bool

    init(){
        self.newBid = false
    }

    func toggleBid(){
        self.newBid = !self.newBid

    }
    func printNew() {
        print(self.newBid.description)
    }
} 

Here is code referencing the ObservedObject in View A @ObservedObject var bid:Bid = Bid()

Here is the code adding the variable and Toggling the value in the View B

@ObservedObject var bid:Bid = Bid() 
    . . .
   print("NeW Bid Flag Before Toggle", bid.newBid.description)
   bid.toggleBid()
   print("NeW Bid Flag Toggled in BidPicker", bid.newBid.description)

I added a button in View A so that I could print the value and the results I see are

Bid Picker Score with bids - Save Complete
NeW Bid Flag Before Toggle false NeW Bid Flag Toggled in BidPicker true 2 started PlayerStack from View A - false

2      

hi Sandy,

your code is saying that View A has defined

@ObservedObject var bid:Bid = Bid()

and View B has also defined

@ObservedObject var bid:Bid = Bid() 

so View A and View B have distinct bids; they are not referencing the same bid.

ideally, you will have a parent view that contains both View A and View B, as in

struct ParentView: View {
  @StateObject var bid: Bid = Bid() // use @StateObject when you create an ObservableObject in a View
  var body { 
    // view stuff here
    ViewA(bid: bid)
    // other view stuff here
    ViewB(bid: bid)
    // perhaps more view stuff
  }
}

and, importantly, your definition of var bid in each of View A and View B should be

@ObservedObject var bid:Bid  // there is no initialization here; it is "passed in"

now the ParentView creates and owns a single Bid object; each of the subviews references it.

hope that helps,

DMG

2      

DMG Yes, I had figured out to remove the initializations in both View A and B, also made the one in view @StateObject, it passes to view B in the call, and now I can see that it is passing from A to B and Back with the correct state change, but it is still NOT updating the view.

I had a 30 min lab/call with the Apple WWDC folks, had a CoreData guy and a SwiftUI guy. They said the changes to core Data should have triggered the view and we made changes to about three views in the viewhierarchy declaring the core data classes as ObservedObjects. Did the ParentData Object, and also the ChildData Object, both, and it still never triggered update before we ran out of time.

I will try to move instantiation of the @StateObject up the view hierarchy as you suggested to see if that helps. Just easier to try to get a single flag to trigger the re-render if possible. Color me trying to stay optomistic...

2      

Moved the @StateObject up the chain, but still no joy.

2      

This initial problem has been solved. I took the published property on the observable/observed class and added it in a hidden text field to the view needed to re-render and it solved the problem.

I have a different/more interesting problem, which I will open in a different thread.

2      

This initial problem has been solved. I took the published property on the observable/observed class and added it in a hidden text field to the view needed to re-render and it solved the problem.

I have a different/more interesting problem, which I will open in a different thread.

2      

Hacking with Swift is sponsored by RevenueCat

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

Learn more here

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.