Apple docs for RealityView state:
You can also use the optional update closure on your RealityView to update your RealityKit content in response to changes in your view’s state."
Unfortunately, I've not been able to get this to work.
All of my 3D content is programmatically generated - I'm not using any external 3D modeling tools. I have an object that conforms to @ObservableObject. Its @Published variables define the size of a programmatically created Entity. Using the initializer values of these @Published variables, the 1st rendering of the RealityView { content in } works like a charm.
Basic structure is this:
var body: some View {
RealityView { content in
// create original 3D content using initial values of @Published variables - works perfect
} update: { content in
// modify 3D content in response to changes of @Published variables - never works
}
Debug statements show that the update: closure gets called as expected - based upon changes in the viewModel's @Published variables. However, the 3D content never changes - even though the 3D content is based upon the @Published variables.
Obviously, if the @Published variables are used in the 1st rendering, and the update: closure is called whenever changes occur to these @Published variables, then why isn't the update: closure updating the RealityKit content as described in the Apple docs?
I've tried everything I can think of - including removing all objects in the update: closure and replacing them with the same call that populated them in the 1st rendering. Debug statements show that the new @Published values are correct as expected when the update: closure is called, but the RealityView never changes.
Known limitation of the beta? Programmer error? Thoughts?
Well, as seems to be all too often and embarassingly the case - ask a question in public, and suddenly the answer reveals itself...
As it turns out I wasn't actually removing all objects in the update: closure and then replacing them - I was adding rebuilding them in my graph but not in the RealityView update: closure. Dumb oversight.
TL;DR: I was expecting the @Published values to automagically update the 3D content - much the same way that SwiftUI automatically updates 2D textfields and such. That isn't what happens in the RealityView update: closure.
So, the solution was simple:
} update: { updateContent in
updateContent.entities.removeAll()
graph.renew()
for element in graph.all {
updateContent.add(element)
}
}
where graph is a custom object that contains my RealityKit scene.