9

I'm using ARKit with SpriteKit. My AR feature is not a core feature in my app, users may optionally navigate to a viewController with an ARSKView where I configure and set an SKScene in the ARsession.

I pause the session when the user navigates back:

override func viewWillDisappear(_ animated: Bool) {
    super.viewWillDisappear(animated)   
    sceneView.session.pause()
}

then the viewController is pop from the navigationController where it was pushed. But I'm seeing that the func update(_ currentTime: TimeInterval) in my SKScene subclass keeps being called... which is the appropriate way to also stop that? I thought that pausing the ARSession would also stop the scene.

EDIT: when I navigate back from the view controller that holds the ARSKView and it is deinitialized, I see that the SKScene is still in memory and a new one is created every time I navigate again to the AR related view controller. So, if I navigate there N times, I see I have N SKScenes in memory. How could I appropiately remove the scene when navigating back? The scene property of the ARsession is read only.

AppsDev
  • 12,319
  • 23
  • 93
  • 186

2 Answers2

3

that sounds like a memory retain issue. Make sure that any node in you SKScene subclass is properly removed and deinitialized. Set your scene delegate to nil, set any node or array of nodes referenced in your VC to nil.

I ran a simple test with 2 VC embedded in a navigation controller, the first one has a view with a start button, the second one is the ARVC. I run the session, set my custom scene delegate to ARVC, when I hit the back button everything stops by itself and my ARVC is being popped. Nothing in memory.

So make sure that you manually remove all your nodes and their animations, constraints, actions and anything that relates to you custom SKScene subclass. Also don't be afraid to call sceneView.presentScene(nil) if that can help.

Adrien Yvon
  • 662
  • 7
  • 18
2

I couldn't find any official documentation about this, apparently there is not proper method to stop a session yet. What I found that was similar to your scenario is this work around.

It suggests you create ARSKView as disposable and before leaving your controller, pause the session, remove the view from superview and assign it to nil.

Marina Aguilar
  • 1,151
  • 9
  • 26
  • Welcome to Stack Overflow! Whilst this may theoretically answer the question, [it would be preferable](//meta.stackexchange.com/q/8259) to include the essential parts of the answer here, and provide the link for reference. – Anton Menshov Sep 17 '19 at 00:38