Swift: Window flickers if .reloadData is called (NSSearchField, NSTableView)

I am using a NSSearchField on top of a NSTableView to filter it contents. Basically it works fine, but the whole window flickers / flashes when .reloadData is called


Image: https://i.stack.imgur.com/dtJ9Z.gif


My search function is pretty straight forward:


    @IBAction func sideBarSearchFieldAction(_ sender: Any) {
   
        DispatchQueue.main.async {

            let searchString = self.sideBarSearchField.stringValue

            if searchString == "" {

                myArrayBuffer = myArray

            } else {

                let filteredArray = myArray.filter { $0.searchString.contains(searchString.lowercased()) }
                // 'myArray' is a collection of struct’s with a var 'searchString'

                myArrayBuffer = filteredArray
                // 'myArrayBuffer' is displayed by the NSTableView

            }
       
            self.sideBarTableView.reloadData()
        }

    }


Would be great if someone could help!

I'm having the same issue. Did you ever determine what was causing this?

What does "the whole window" mean? The content area? The title bar and other chrome? Are there any parts of the window that don't flicker?


Calling reloadData will cause the entire table view to be erased to its background color before the rows are re-drawn, so obviously the table view itself is going to "flicker". If more of the window content is getting erased, I'd start to suspect that the table view is incorrectly sized. (You might not notice, normally, because all the columns could be visible, but the table view itself could be wider.)


I'd suggest you try using Xcode's view debugger to examine the structure of your window:


developer.apple.com/library/content/documentation/DeveloperTools/Conceptual/debugging_with_xcode/chapters/special_debugging_workflows.html

Swift: Window flickers if .reloadData is called (NSSearchField, NSTableView)
 
 
Q