Questions tagged [observedobject]

@ObservedObject is a SwiftUI property wrapper used to mark properties that are not owned by the View, but should update the View. Only ObservableObject conformant types can be annotated with the @ObservedObject property wrapper.

For more info, see the official documentation of @ObservedObject.

173 questions
1
vote
1 answer

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

I am a fairly novice developer. I wrote an app for tracking indoor bouldering climbs all in 1 really large file. It was working fine, just hard to maintain. Now I am working on splitting it into multiple files and having some issues. I created this…
Gil
  • 173
  • 1
  • 15
1
vote
1 answer

Published Bool variable resetting automatically

I have the following simple SwiftUI view which contains a component containing a list of marketing preference options with checkboxes and a button to update the preferences: @StateObject var marketingPreferencesViewModel:…
DevB1
  • 1,235
  • 3
  • 17
  • 43
1
vote
1 answer

Use of @StateObject in Document-based SwiftUI application: getting instances instead of references

I am trying to use a @Stateobject in my document as source of truth for the dataModel behind the document. However, it seems I am doing something wrong as I appear to get different instances for the document and the content view, which is in line…
1
vote
1 answer

How to initialize `var` that depends on `environmentObject` in SwiftUI View init()?

Is there a more natural way (now that it's 2022) of initializing some internal variables that depend on an @EnvironmentObject (or other @ObservedObject) within a view's init() function? For example, the following shows what I'm trying to do…
Zaphod
  • 1,387
  • 2
  • 17
  • 33
1
vote
1 answer

SWIFTUI: Why adding @State to property doesn't update data model

having a problem with updating data in ObservedObject here's the data MRE: struct Property: Identifiable, Codable, Hashable { var id = UUID() var name : String = "" var meters : [Meter] = [Meter(name: "electricity"), Meter(name:…
Roman R
  • 39
  • 8
1
vote
0 answers

Nested NavigationLink selection bound to ObservedObject: Navigation animation shown twice

I have a List of NavigationLinks (2nd level) with their selection bound to an @ObservedObject's value, so I can navigate programmatically by setting stuff.selectedMessage. struct MessagesView: View { @ObservedObject var stuff: MyStuff var…
nQk
  • 165
  • 1
  • 2
  • 6
1
vote
0 answers

view automatically pop to previous view in swift ui due to view model is observed from tabview to inside multiple view?

I am facing an issue while pushing 1 view to another view. Let me explain the hierarchy. ContentView -> 2 tabs, TabAView and TabBView Inside TabBView. There is 1 view used ConnectView: Where is a Button to connect. After tapping on the button of…
1
vote
1 answer

SwiftUI - How can I use ObservedObject or EnvironmentObject to store GeometryReader data?

I am trying to follow the design created for an app which has some objects placed in the middle of the screen. The objects should have a size and padding proportional to the device's screen size, meaning they should appear bigger if the screen is…
Tomas
  • 153
  • 1
  • 1
  • 12
1
vote
1 answer

Changing View's ViewModel on button tap causes: "AttributeGraph: cycle detected through attribute warning"

I made a simple Playground example trying to figure how connecting View Model to the View works with SwiftUI and Combine. I believe I've done it by the book (check the code example below) but when I tap on the "Increment" button this print appears…
1
vote
2 answers

My ObservableObject Class Array Variable 'timeStep' Fails To Hold Values

I have two ObservableObject classes: CarLotData and StopWatchManager. What I am trying to do is use my StopWatchManager class to load CarLotData property timeStep--which is a array Double type. Here is my code for StopWatchManager: import…
gosborne3
  • 47
  • 1
  • 9
1
vote
2 answers

SwiftUI: ObservedObject/ObservableObject weird behavior

I recently encountered the following problem in SwiftUI with ObservedObject/ObservableObject: If the ObservedObject/ObservableObject is publishing in a View, the body property is recalculated - as expected. But if there is a sub View in the body…
lucaszischka
  • 132
  • 1
  • 1
  • 7
1
vote
0 answers

Write Optimization in SwiftUI updating EnvironmentObject

I have an array inside my EnvironmentObject that drives a few of my views. Now I need an update function for that array. Some elements might change, some may not. Should I first check each element whether it "needs" the update and only write to the…
Gin Tonyx
  • 383
  • 1
  • 11
1
vote
0 answers

@ObservedObject changes not reflecting after an async operation

I am a beginner at Swift and I've roughly tried to create an MVVM kinda architecture based on some tutorials However I find that the @ObservedObject variable wallpaperViewModel having some @Published value properties doesn't reflect the changes…
gtxtreme
  • 1,830
  • 1
  • 13
  • 25
1
vote
2 answers

Data from ObservedObject not rendered in SwiftUI view

I use combine to connect to a REST API which pulls some data into an ObservableObject. The whole setup is really just MVVM. The ObservableObject is then being Observed in a view. Now, I'm stuck with a bug I can't seem to get resolved. What seems to…
DrGonzoX
  • 363
  • 4
  • 14
1
vote
1 answer

SwiftUI update observedObject in Cell View of list

I tried to change the value of the observedObject. The value will changed but not permanently. When I scroll down and scroll back, the value will changed back to original value. I guess is the problem of @State in the cell view of list. But I cant…
Aatrox11
  • 65
  • 4