I have looked here: Reality View Documentation
Found this thread: RealityView Update Closure Thread
I am not able to find documentation on how the update closure works.
I am loading attachments using reality view's attachment feature (really helpful). I want to remove them programmatically from another file. I found that @State variables can be used. But I am not able to modify them from out side of the ImmersiveView swift file. The second problem I faced was even if I update them inside the file. My debugging statements don't execute.
So exactly when does update function run. I know it get's executed at the start (twice for some reason). It also get's executed when I add a window using:
openWindow?(id: "ButtonView")
I need to use the update closure because I am also not able to get the reference to RealityViewAttachment outside the RealityView struct.
My Code(only shown the code necessary. there is other code):
@State private var pleaseRefresh = ""
@StateObject var model = HandTrackingViewModel()
var body: some View {
RealityView { content, attachments in
if let immersiveContentEntity = try? await Entity(named: "Immersive", in: realityKitContentBundle) {
content.add(immersiveContentEntity)
}
content.add(model.setupContentEntity())
content.add(entityDummy)
print("View Loaded")
} update: { content, attachments in
print("Update Closure Executed")
if (model.editWindowAdded) {
print("WINDOW ADDED")
let theattachment = attachments.entity(for: "sample")!
entityDummy.addChild(theattachment)
// more code here
}
}
attachments: {
Attachment(id: "sample") {
Button(action: {
model.canEditPos = true
model.canRotate = false
pleaseRefresh = "changed"
}) {
HStack {
Image(systemName: "pencil.and.outline")
.resizable()
.scaledToFit()
.frame(width: 32, height: 32)
Text("Edit Placement")
.font(.caption)
}
.padding(4)
}
.frame(width: 160, height: 60)
}
}
How can the update method (or the code inside it) run when I want it to? I am new to swift. I apologize if my question seems naive.