@ObservedObject Issue

I have a major issue with @ObservedObject.


I have the following statement

@ObservedObject var model: UserData

in a good number of views and before Beta 5, I did not have to pass this variable into the preview, nor did I have to pass his variable into other views which already have the above statement.


But now I do have to and it's a mess. Is there anyway around this?

Accepted Reply

The SwiftUI Apple tutorial is using @EnvironmentObject instead of @ObservedObject. That appears to be working for me.

Replies

I have the same concern, having duly crammed parameter passes everywhere.


Moreover, at one time this all worked well using BindableObject on the class and @ObjectBinding on the objective truth objects. When beta 5 arrived, I converted to ObservableObject, Identifiable and @ObservedObject. Having done that, the views no longer update. I've verified the external events happen, the objective truth variable changes, and the willChange.send() fires, but the views remain unsullied by any updates.


My other two big gripes are (1) live views are missing, and (2) I can discover no apparent SwiftUI way to respond to orientation changes.

I'm guessing #1 is because of the documented incompatbility with Xcode 11b5 and Catalina b4. Hopefully Catalina b5 resolves that when it drops (today?).

Same error here after installing Catalina Beta 5 😟

The SwiftUI Apple tutorial is using @EnvironmentObject instead of @ObservedObject. That appears to be working for me.

Don't forget to implement:

let objectWillChange = ObservableObjectPublisher()

in your ObservableObject class

I am also having troubles with anything relating to @ObservableObject and I see some effort to use @EnvironmentObject instead -- but this requires some amount of making changes and I notice there is no xcode update to go with the Beta?


Has anyone from Apple verified this? Seems like a major bug late in the beta?

SpaceMan, can you help us understand what you did exactly and what you think is going on with the @ObservedObject protocol?


I kind of assume since this is Xcode beta 5 installing onto Beta 6 and failing while working as expected on the beta 5 Simulator that this is a bug in the Beta 6 iOS release and we may get an update or the Xcode will be updated very shortly to resolve this.


Do you think this is a permanent change?

You don't need to do this. Check out the SwiftUI tutorials again. You'll see the changes that they made.

Check out the following website describing the differences between @EnvironmentObject and @ObservedObject.


https://mecid.github.io/2019/06/12/understanding-property-wrappers-in-swiftui/

I had exactly the same problem with macOS 10.15 Beta5. Neither @ObservableObject nor @EnvironmentObject in a View (SwiftUI) worked. Changing a parameter in the observed object (or environment object) and sending the message from its objectWillChange Publisher did not result in updating the View.


I could solve this problem in my code with one single line:

adding "let objectWillChange: ObservableObjectPublisher = ObservableObjectPublisher()" to the observed object (environment object).


Even though there is a default implementation of this variable after adding "ObservableObject" protocol declaration it seems not to work properly.


After adding this line everything worked as expected. Before no message ever reached the subscribers.

@ObservableObject does not work on any device running Beta 6 that I have been able to test.


We're using xCode beta 5 which is odd, but @ObservableObject code runs as expected while using xcode and simulator.


ObservableObject does not seem to work on Watch simulator at all.



Can anyone confirm this? Apple?



seems like this would be in the release notes or noted somewhere?

If this happens to everyone, would't there be tons of posts on here?


Can anyone confirm that most folks do not have this issue?


Anything else known at this time?

Just use @EnvironmentObject instead of @ObservableObject

That should fix it.