0

I'm trying to hide a UITextView when the floor is detected in ARScene. The code is as below:

class ViewController: UIViewController, ARSCNViewDelegate {

    ...

    @IBOutlet var sceneView: ARSCNView!
    @IBOutlet weak var myTextView: UITextView!

    // runs on floor detection
    func renderer(_ renderer: SCNSceneRenderer, didAdd node: SCNNode, for anchor: ARAnchor) {

        if anchor is ARPlaneAnchor {
            myTextView.isHidden = true // Error from Main Thread Checker 
        }
    }

    ...
}

However, it doesn't hide myTextView but produces an error saying UIView.hidden must be used from main thread only. I'd like to know what the right approach would be. I'm new to Swift.

cypark
  • 838
  • 6
  • 21

1 Answers1

1

That is because renderer runs in background thread. You need to run all code related to ui in main thread.

DispatchQueue.main.async {
    myTextView.isHidden = true
}
Alok Subedi
  • 1,601
  • 14
  • 26
  • you just solved my issue. thanks!! can i use this static method as many times as needed? or is this just a quick fix? – cypark Jan 31 '18 at 04:51
  • 1
    This sends your code to execute from main thread. So you can use it every time you need to run codes in main thread from inside background thread – Alok Subedi Jan 31 '18 at 04:53
  • Awesome. Thanks again. – cypark Jan 31 '18 at 04:54
  • Not question the fact it runs in the background thread, but I'm not able to find any documentation stating that. I've a snippet of code here that works ok without going on the main thread. – Chi-Hwa Michael Ting Apr 02 '19 at 23:41
  • @Chi-HwaMichaelTing can you share the code snippet? – Alok Subedi Apr 05 '19 at 06:14
  • func renderer(_ renderer: SCNSceneRenderer, didAdd node: SCNNode, for anchor: ARAnchor) { guard let planeAnchor = anchor as? ARPlaneAnchor else { return } DispatchQueue.main.async { [weak self] in if let planeNode = self?.createARPlaneNode(planeAnchor: planeAnchor, color: UIColor.yellow.withAlphaComponent(0.5)) { node.addChildNode(planeNode) } } } – Chi-Hwa Michael Ting Apr 16 '19 at 18:00
  • Your code inside `createARPlaneNode(planeAnchor):`, if I am not wrong, doesnot handle UI. So it should run normally. And handling scenekit codes run inside `renderer` thread – Alok Subedi Apr 17 '19 at 05:47