Swift ARKit How do you fully kill an ARSession?
Asked Answered
A

2

17

I have an ARSCNView running an ARSession. You can pause the session with session.pause() sure, but that still in effect, leaves the session running. I have tried deallocating the ARSCNView by removing it from its superview. The ARSCNView indeed deallocates but the ARSession is still running afterwards!! You can't assign nil to ARSession either. I can see the ARSessionDelegate's

func session(_ session: ARSession, didUpdate frame: ARFrame) 

is still being called!

How do you completely wipe the slate clean with ARKit once you have finished with it?

Is it even possible?

Artima answered 11/10, 2018 at 21:40 Comment(6)
What are you trying to achieve on didUpdate?Fustic
Nothing, it’s just a way of seeing that the session is still running.Artima
Did you find any solution for this?Shoup
Apple don’t provide a way to sensibly end the ARSession at present. But I did find a work-around which works.Artima
@GeoffH I have the same question, do you mind sharing the workaround that works for you? For me, I am trying alternate between arsession and an avcapture session. I see strange behavior and suspect it’s related to the presence of arsessionAddison
@Addison In short, you make the ARSCNView hierarchy disposable. The other non-AR areas of your app would be in separate view hierarchies at a sibling level to your ARSCNView. When you need AR mode, you build the ARSCNView hierarchy (and subsequently start an ARSession). When you’re done, you deallocate the entire ARSCNView hierarchy and switch to the other view hierarchy in your app. Rebuild again when going back to AR. I wish there was a more intuitive way provided by Apple. But at the time of writing, there isn’t. This is the next best thing though.Artima
A
11

Officially, right now, you can't.

However, there is a work-around: you make the ARSCNView disposable.

On leaving AR, first pause the ARSession. Then deallocate the entire ARSCNView hierarchy & set ARSCNView to nil for good measure. Rebuild the entire ARSCNView hierarchy and start a new session whenever you need to go back to AR.

var ARview: ARSCNView?

func punchTheClown() {

    ARView?.session.pause()
    ARView?.removeFromSuperview()
    ARView = nil

}

Other non-AR areas of your app would typically be in a separate view hierarchy at the same sibling level as your ARSCNView. I look forward to Apple providing an actual stopSession() function, so we can all stop having to punchTheClown in the meantime.

Artima answered 25/12, 2018 at 6:0 Comment(4)
This doesn't work for me. I could see the session being active even after I set the scene view object to nil. Any other suggestions?Sadiron
Same problem on RealityKit´s ARView(). It doesn't work for me either, not found a way to deallocate it from memory at all.Dad
Doesn't work. Even did arView.window?.resignKey(), arView.scene.anchors.removeAll(), arView.session.delegate = nil. Somehow it still stays around, and when creating a new ARView, you get the yellow "Tap to Resume"Beitnes
Now This solved my issueDebbydebee
F
2

From the docs

While paused, the session doesn't track device motion or capture scene imagery, nor does it coordinate with its delegate object or update any associated ARSCNView or ARSKView object

If it isn’t causing issues, better be left alone. As far as I know. This works like a video player. You can Pause and resume it anytime

Source here: .RunOptions ARKit Docs

Fustic answered 11/10, 2018 at 22:3 Comment(2)
Good to know. Sometimes, you can't afford to just leave it alone though. I need to free-up the resources. The app is not solely spent in augmented reality. The SCNSceneRenderer for the ARSCNView is killing all updating of other things & will not stop unless you destroy the ARSCNView. How to we kill the session?Artima
This no longer works as of iOS 17 — tracking continues totally unaffected by calling .pause()!Schizomycete

© 2022 - 2024 — McMap. All rights reserved.