I have the following piece of code:
@State var root = Entity()
var body: some View {
RealityView { content, _ in
do {
let _root = try await Entity(named: "Immersive", in: realityKitContentBundle)
content.add(_root)
// root = _root <-- this doesn't trigger the update closure
Task {
root = _root // <-- this does
}
} catch {
print("Error in RealityView's make: \(error)")
}
} update: { content, attachments in
// NOTE: update not called when root is modififed
// unless root modification is wrapped in Task
print(root)
// the intent is to use root for positioning attachments.
} attachments: {
Text("Preview")
.font(.system(size: 100))
.background(.pink)
.tag("initial_text")
}
} // end body
If I change the root
state in the make
closure by simply assigning it another entity, the update closure will not be called - print(root)
will print two empty entities. Instead if I wrap it in a Task
, the update closure would be called: I would see the correct root
entity being printed.
Any idea why this is the case?
In general, I'm unsure the order in which the make
, update
and attachment
closures are executed. Is there more guidance on what we should expect the order to be, what should we do typically in each closure, etc?