Do you retain a reference to your content events in RealityView?

Do you retain a reference to your content (RealityViewContent) events? For example, the Manipulation Events docs from Apple use _ to discard the result. In theory the event should keep working while the content is alive.

_ = content.subscribe(to: ManipulationEvents.WillBegin.self) { event in
event.entity.components[ModelComponent.self]?.materials[0] = SimpleMaterial(color: .blue, isMetallic: false)
}
_ = content.subscribe(to: ManipulationEvents.WillEnd.self)  { event in
    event.entity.components[ModelComponent.self]?.materials[0] = SimpleMaterial(color: .red, isMetallic: false)
}

We could store these events in state. I've seen this in a few samples and apps.

@State var beginSubscription: EventSubscription?

...

beginSubscription = content.subscribe(to: ManipulationEvents.WillBegin.self) { event in
    event.entity.components[ModelComponent.self]?.materials[0] = SimpleMaterial(color: .blue, isMetallic: false)
}

The main advantage I see is that we can be more explicit about when we remove the event. Are there other reasons to keep a reference to these events?

Answered by DTS Engineer in 858444022

Hello @radicalappdev,

The main advantage I see is that we can be more explicit about when we remove the event.

Correct, if you store the subscription, you can cancel it explicitly.

Are there other reasons to keep a reference to these events?

I'm not aware of any other reason you might want to store the subscription.

-- Greg

Accepted Answer

Hello @radicalappdev,

The main advantage I see is that we can be more explicit about when we remove the event.

Correct, if you store the subscription, you can cancel it explicitly.

Are there other reasons to keep a reference to these events?

I'm not aware of any other reason you might want to store the subscription.

-- Greg

Do you retain a reference to your content events in RealityView?
 
 
Q