2

我有以下代码可以完美运行11.4.1但失败了12

let background = DispatchQueue(label:"task")
var debugMeshNode = SCNNode()
let myKit = MyKit()

func renderer(_ renderer: SCNSceneRenderer, didAdd node: SCNNode, for anchor: ARAnchor) {
    self.background.async {
        let node = self.myKit.extractNode(anchor:anchor)
        self.debugMeshNode.addChildNode(node) // no node added on UI in iOS12
    }
}

func session(_ session: ARSession, didUpdate frame: ARFrame) {
    self.background.async {
        self.myKit.process(frame: frame)
    }
}

谁能在这里指出我的错误?

更新

如果我像这样在块中添加打印语句,代码似乎可以工作,

func renderer(_ renderer: SCNSceneRenderer, didAdd node: SCNNode, for anchor: ARAnchor) {
    self.background.async {
        let node = self.myKit.extractNode(anchor:anchor)
        self.debugMeshNode.addChildNode(node) // no node added on UI in iOS12
        print("sample")
    }
}
4

1 回答 1

0

原来从这里,我用这个

func guaranteeMainThreadSynchronousExecution(_ block: () -> ()) {
    if Thread.isMainThread {
        block()
    } else {
        DispatchQueue.main.sync {
            block()
        }
    }
}

并像这样更新了我的代码,

func renderer(_ renderer: SCNSceneRenderer, didAdd node: SCNNode, for anchor: ARAnchor) {
    self.guaranteeMainThreadSynchronousExecution {
        self.background.async {
            let node = self.myKit.extractNode(anchor:anchor)
            self.debugMeshNode.addChildNode(node) // no node added on UI in iOS12
        }
    }
}

然后它完美地工作。希望这可以帮助某人。

于 2018-10-10T15:16:00.503 回答