ARKit: pausing session does not stop the scene to be called
Asked Answered
P

2

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.

Popelka answered 11/10, 2017 at 7:9 Comment(8)
pause the view, sceneView.isPaused = true, or pause the scene, sceneView.scene.isPaused = true depending on what you needStair
@Stair thank you. However, since my app is not focused in ARKit but an additional feature, user can navigate back from the view controller that holds the ARSKView. I've noticed that pausing the SKScene is not enough, it still lives after the view controller is deinit, how should I "destroy" the scene?Popelka
@Popelka did you find a solution?Gallonage
@Stair there doesn't appear to be isPaused for SCNView, only pause()Gallonage
@Gallonage this is about SKViewStair
@Stair correct you are! apologies. :)Gallonage
@Popelka did you find any solution?Redman
I mean who would ever want to properly stop an ARSession, once started hey. Nobody surely ;) -Yeah, this is a fairly major oversight on Apple's part. @Popelka Did you figure this out?Nephrotomy
H
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.

Howlett answered 17/9, 2019 at 12:55 Comment(0)
D
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.

Diclinous answered 17/9, 2019 at 0:18 Comment(1)
Welcome to Stack Overflow! Whilst this may theoretically answer the question, it would be preferable to include the essential parts of the answer here, and provide the link for reference.Aardwolf

© 2022 - 2024 — McMap. All rights reserved.