What is the difference between sceneView.delegate and sceneView.session.delegate?

Viewed 817
1 Answers

The sceneView delegate is of type ARSCNViewDelegate, while the session delegate is of type ARSessionDelegate. As you can see in the documentation, they provide different information through their methods, but they also provide some overlapping functionality, since both extend ARSessionObserver.

You will want to implement ARSCNViewDelegate only when you're working with ARSCNView, which is the case of the linked project. Most of the methods provided by this delegate are related to updates on the objects displayed by the renderer (SceneKit). So, when you're using ARSCNView, SceneKit and ARKit are tied together. When ARKit session (sceneView.session) is updated, the renderer is informed, and then it is updated, triggering methods from ARSCNViewDelegate. For example, when an anchor is added by ARKit, and a node is created associated with that anchor, renderer(_:didAdd:for:) will be called.

As for ARSessionDelegate, you will want to implement it when you need to know about anchor changes in the session, or when new frames arrive from the camera feed. These updates are not tied to any renderer. This means that you can implement these methods with a different renderer, like Metal, for example. You would only need to create an ARSession object and set its delegate.

Since both protocols extend from ARSessionObserver, I would say that you will almost always implement only ARSCNViewDelegate when you're using an ARSCNView. The only method that could possibly benefit you from ARSessionDelegate is session(_:didUpdate:), which informs you about frame updates, and it seems that there is no equivalent in ARSCNViewDelegate. As for anchor updates, they will be reflected on ARSCNViewDelegate since SceneKit will update its scene based on ARKit events.

One last thing: ARSCNView is just a convenience class that Apple provided so you already have ARKit tied with a SceneKit renderer, but it would still be possible for you to implement your own ARSession with a custom SCNScene.

Related