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 SKScene
s in memory. How could I appropiately remove the scene when navigating back? The scene
property of the ARsession
is read only.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With