Swiftui View Does Not Updated When Observedobject Changed

SwiftUI: View not updating when Observed object changes from another file

There are many ways to have the viewModel changes reflected in different Views, the following is an example, where the main idea is to have one source of truth that you use in all the different Views:

declare @StateObject var viewModel = VariableSetupModel() in ContentView and
@EnvironmentObject var viewModel: VariableSetupModel in ContentNavView.

Pass viewModel from
ContentView to ContentNavView using .environmentObject(viewModel), that is, add this to ContentView, eg: VStack (alignment: .leading) {...}.environmentObject(viewModel)

Note, there is no need for VariableSetupModel to be a singleton, remove the static let shared = VariableSetupModel().

Note also, to have the viewModel available in SettingsView() use, SettingsView().environmentObject(viewModel)

Another approach is to pass a ObservedObject from one view to another.
For example:

declare @StateObject var viewModel = VariableSetupModel() in ContentView and

@ObservedObject var viewModel: VariableSetupModel in ContentNavView.

Pass viewModel from ContentView to ContentNavView using ContentNavView(viewModel: viewModel).

SwiftUI Observed Object not updating

At the moment you receive the setHeightViewModel in various views as a var,
you should receive it as an ObservedObject.

I suggest you try this, in ViewSetHeight

@StateObject var setHeightVM = setHeightViewModel()  

and in all your other views where you pass this model, use:

@ObservedObject var viewModel: setHeightViewModel

such as in ModeArea, SwitchImperial, SwitchMetric, SelectionUp, etc...

Alternatively you could use @EnvironmentObject ... to pass the setHeightViewModel
to all views that needs it.

SwiftUI ObservableObject not updating when value is Published

You are likely winding up with different UsersViewModel objects:

@ObservedObject var usersViewModel = UsersViewModel()

Since UsersView is a struct, this creates a new model every time the View is instantiated (which can happen very often, not just when the view appears). In iOS 14 there is @StateObject to combine State (which preserves information between View instantiations) with ObservedObject, but in iOS 13 I recommend passing in the ObservedObject if it's not a shared instance.

SwiftUI Observed Object not updating when published value changes

The ForEach does not detect that any of cards changes because it is uses Equatable which in your case uses only id.

Here is a fix:

struct Card: Identifiable, Equatable {
var isFaceUp: Bool = false
var isMatched: Bool = false
var content: CardContent

var id: Int

static func == (lhs: MemoryGame<CardContent>.Card, rhs: MemoryGame<CardContent>.Card) -> Bool {
lhs.id == rhs.id && lhs.isFaceUp == rhs.isFaceUp // << here !!
}
}

and also needed update for

mutating func choose(_ card: Card) {
let chosenIndex = cards.firstIndex{ $0.id == card.id } // << here !!
cards[chosenIndex!].isFaceUp.toggle()
}

Tested with Xcode 13.4 / iOS 15.5

SwiftUI: Array Not Updating In All Views When Referencing The Same Observed Object

Ok I got it working. All I had to do was move the functions I had in my view to my viewModel. Im guessing to keep everything in the same scope. My guess is that everything in ContentView was it's own copy. I'm not 100% certain on that, but it works now.



Related Topics



Leave a reply



Submit