-1

I need to read a checkbox’s state from a thread that is not the main thread. It seems I cannot simply use the following (which causes the Main Thread Checker to say “UI API called on a background thread: -[NSCell state]”):

myCheckbox.state

What am I supposed to do instead?

1 Answers1

1

My current solution is to maintain a property that gets updated when the checkbox gets switched, and access that property instead of directly reading the checkbox’s state.

Something more elegant would be welcome: this is cumbersome; even more so if you want to make the property read-only or properly handle setting its value from inside the program.

class myViewController: NSViewController {

    @IBOutlet private weak var myCheckbox: NSButtonCell!
    public var myCheckboxState: Bool! // This can be read from any thread.

    @IBAction func onMyCheckboxAction(_ sender: NSButtonCell) {
        myCheckboxState = (myCheckbox.state == NSOnState)
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        ...
        myCheckboxState = (myCheckbox.state == NSOnState)
        ...
    }

    ...
}