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
25
votes
1 answer

What is the difference between @EnvironmentObject and @ObservedObject?

I have been reading about the property wrappers in SwiftUI and I see that they do a great job, but one thing which I really don't get is the difference between @EnvironmentObject and @ObservedObject. From what I learned so far, I see that…
Dakata
  • 1,227
  • 2
  • 14
  • 33
12
votes
2 answers

SwiftUI: How can I catch changing value from observed object when I execute function

I have a problem with observed object in SwiftUI. I can see changing values of observed object on the View struct. However in class or function, even if I change text value of TextField(which is observable object) but "self.codeTwo.text still did…
alice_ix
  • 199
  • 1
  • 2
  • 8
5
votes
1 answer

Updating the UI for a tree structure in SwiftUI

I have an ObservableObject class, Model0 which has a Published object nested. The nested object represents a recursive tree structure. I want to be able to update the tree structure and have the UI update appropriately. However, in the below code,…
BenJacob
  • 957
  • 10
  • 31
5
votes
3 answers

SwiftUI ObservableObject not updating when value is Published

I have a view and a viewModel that should update the ListView when users are added to the user array. I can verify that users are being added, yet the ObservedObject is not updating. I have a search bar that lets you search users and then updates…
connorvo
  • 761
  • 2
  • 7
  • 21
5
votes
3 answers

Change on ObservableObject in @Published array does not update the view

I've been struggling for hours on an issue with SwiftUI. Here is a simplified example of my issue : class Parent: ObservableObject { @Published var children = [Child()] } class Child: ObservableObject { @Published var name: String? …
user2742293
  • 59
  • 1
  • 4
4
votes
1 answer

How to use published optional properties correctly for SwiftUI

To provide some context, Im writing an order tracking section of our app, which reloads the order status from the server every so-often. The UI on-screen is developed in SwiftUI. I require an optional image on screen that changes as the order…
Chris G
  • 81
  • 1
  • 7
4
votes
1 answer

Notifications when an ObservedObject is changed

I have a graphical user interface made of several SwiftUI components (lets call them subviews). These subviews communicate with each other with the help of ObservableObject / ObservedObject. When changes are made to one view, the other view is…
TalkingCode
  • 13,407
  • 27
  • 102
  • 147
4
votes
0 answers

How to stop an observedObject from updating the UI in SwiftUi?

I am working with a navigationView with swiftUI. My problem is whenever I go to another view with a Navlink and the observedObject of the root of my navigationView changes, my interface come back to the root view also. So my question is : Is there…
sofiane_dv
  • 71
  • 7
4
votes
1 answer

SwiftUI List freezes on @ObservedObject update

I have the List which automatically fetches more data near the end: struct AdCardListView: View { @ObservedObject var model: AdListViewModel = AdListViewModel() var body: some View { List {…
4
votes
2 answers

How to trigger an action as soon as a SwiftUI observed variable changes its value

I've got a List in my View where its elements are going to be updated as soon as the list argument will change. This is my View: struct ContentView: View { @ObservedObject var users = Utenti() @State private var isSharePresented: Bool = false var…
4
votes
1 answer

What is the difference between @State and @ObservedObject, can they both be used to persist state?

When I Googled "State vs ObservedObject" the first result was from Hacking with Swift and it said about @ObservedObject: This is very similar to @State except now we’re using an external reference type rather than a simple local property like a…
Gil Birman
  • 35,242
  • 14
  • 75
  • 119
3
votes
1 answer

SwiftUI Observed Object not updating when published value changes

I was following along with this lecture when I ran into some problems with my Observed Object updating. I have an @ObservedObject called EmojiMemoryGame with a published MemoryGame variable called model. 'MemoryGame' is a struct that stores…
3
votes
1 answer

Why can't we use existential types as types of properties wrapped with @ObservedObject

protocol ExampleStore: ObservableObject { var text: String { get } } class ConcreteExampleStore: ExampleStore { @Published var text: String = "" } struct ExampleView: View { @ObservedObject private var store: any ExampleStore …
3
votes
1 answer

SwiftUI changing environment object re-created observed object in same view

So, I have few steps, last one contains EnvironmentObject and ObservedObject. The issue is, when I try to modify EnvironmentObject (lane 68) it re-creates ObservedObject. Can any one explain me why this happens? Any solution to keep my…
3
votes
2 answers

SwiftUI observed object do action when its property change

I have observed object which is created in view and I want to have function that will occur when the object bool property is changed. I want something similar to .onTapGesture. Is there a way to do it? have function where the body of the function is…
K. Ondřej
  • 55
  • 8
1
2 3
11 12