I have a very large (500mb) .dae file which takes a few seconds to load, so I load it once in the background and reuse it in two identical view controllers. The first controller shows the model fine, but when I modally present the next view controller (which also shows the model fine) and dismiss it, the model is gone, removed from the first view controller's scene. Does SceneKit try to dispose of Nodes when a SCNView is cleaned up? I'm not sure how else I could explain what is happening, because the two view controllers are two instances of the same class being used as child view controllers.
// CarLoader.swift
class CarLoader {
private static let instance = CarLoader();
var storedCar: SCNNode = SCNNode();
var loaded: Bool = false;
var onReady:(car: SCNNode)->() = { (car: SCNNode) in } ;
private init(){}
class func load( onLoad: ()->() ){
if CarLoader.instance.loaded {
onLoad();
return;
}
dispatch_async(dispatch_get_global_queue(Int(QOS_CLASS_USER_INITIATED.rawValue), 0)) {
let scene = SCNScene(named: "Scenes.scnassets/export_test6.dae");
dispatch_async(dispatch_get_main_queue()) { // 2
let nodeArray = scene!.rootNode.childNodes;
let centeringNode = SCNNode();
for childNode in nodeArray {
centeringNode.addChildNode(childNode as SCNNode)
}
centeringNode.eulerAngles.x = -Float(M_PI/2);
var min = SCNVector3(), max = SCNVector3();
let _ = centeringNode.getBoundingBoxMin(&min, max: &max);
let w = CGFloat(abs(max.x - min.x));
let h = CGFloat(abs(max.y - min.y));
//let l = CGFloat(abs(max.z - min.z));
centeringNode.position.x = -min.x + -Float(w/2);
centeringNode.position.y = -min.y/2 + -Float(h/2); // it's still a little low in y, but we want it lower
centeringNode.position.z = 0;
CarLoader.instance.storedCar = centeringNode;
CarLoader.instance.loaded = true;
CarLoader.instance.onReady(car: centeringNode);
onLoad();
}
}
}
class func cachedModel( onReady: (car: SCNNode) -> ()) {
CarLoader.instance.onReady = onReady;
if CarLoader.instance.loaded {
CarLoader.instance.onReady(car: CarLoader.instance.storedCar);
}
else {
// callback is called when ready
}
}
}