I am running an ARKit Session where I place SceneKit nodes. With every node I am instantiating a new view controller and pass its view as the node's content like so:
func createTextNode(anchor: ARCardAnchor) -> SCNNode? {
let plane = SCNPlane()
plane.height = 0.5
plane.width = 0.5
let sb = UIStoryboard(name: "Main", bundle: nil)
let fCVC = sb.instantiateViewController(withIdentifier: "CardViewController") as! CardViewController
plane.firstMaterial?.diffuse.contents = fCVC.view
let cardNode = SCNNode(geometry: plane)
cardNode.constraints = [billboardConstraint]
return cardNode
}
I am adding the nodes to the scene using the following ARSCNViewDelegate method and my custom ARCardAnchor (a subclass of ARAnchor):
func renderer(_ renderer: SCNSceneRenderer, didAdd node: SCNNode, for anchor: ARAnchor) {
if let fcanchor = anchor as? ARCardAnchor {
DispatchQueue.main.async {
guard let n = self.nodeCreator.createTextNode(anchor: fcanchor) else { return }
node.addChildNode(n)
}
}
}
So far everything works and the nodes are placed in 3D space. But when I navigate back to the previous View Controller the UI freezes and I can't do anything. I've tried using an unwind segue like this
@IBAction func goBackToPrevious(_ sender: Any) {
sceneView.session.pause()
self.performSegue(withIdentifier: "unwindToPrevious", sender: self)
}
and a navigation controller where I pop the AR scene controller off the stack. Every time the previous view controllers are frozen. No errors in Xcode, the app keeps running . If I wait for approx. 2 minutes I can use the screen again. If I don't add nodes with view controllers to my AR scene, everything works perfectly fine. My only explanation is that the UIThread is overwhelmed when adding nodes because it creates a massive memory leak somewhere (that I haven't found despite of 10 hours of debugging). Has anyone had a similar experience and can tell me how to resolve this? What can I do to debug this and ensure smooth navigation?