15

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?

Geoff H
  • 3,107
  • 1
  • 28
  • 53
  • What are you trying to achieve on didUpdate? – excitedmicrobe Oct 11 '18 at 21:59
  • Nothing, it’s just a way of seeing that the session is still running. – Geoff H Oct 11 '18 at 22:00
  • Did you find any solution for this? – halfblood17 Dec 11 '18 at 13:54
  • Apple don’t provide a way to sensibly end the ARSession at present. But I did find a work-around which works. – Geoff H Dec 12 '18 at 05:50
  • @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 arsession – kawingkelvin Dec 24 '18 at 17:24
  • @kawingkelvin 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. – Geoff H Dec 25 '18 at 05:34

2 Answers2

10

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.

Geoff H
  • 3,107
  • 1
  • 28
  • 53
  • 3
    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? – The X-Coder Sep 26 '19 at 06:21
  • 3
    Same problem on RealityKit´s ARView(). It doesn't work for me either, not found a way to deallocate it from memory at all. – HelloTimo Jan 25 '20 at 11:17
  • 1
    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" – 123 Mar 06 '21 at 20:18
  • 1
    Now This solved my issue – Vivek Feb 03 '22 at 06:14
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

excitedmicrobe
  • 2,338
  • 1
  • 14
  • 30
  • 3
    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? – Geoff H Oct 12 '18 at 08:44