I have a window that displays some data in an NSTableView
. This data is loaded in the background. The data-loading-thread is started in the windowDidLoad:
method. If the window is closed before loading has finished, the background thread should be cancelled. I do this by signalling the thread in the windowWillClose:
delegate method and waiting for the background thread to finish.
Now this all works perfectly. But I have one problem: How can I update the data in the table view? I have tried calling reloadData
via performSelectorOnMainThread:
but this leads to a race condition: The reloadData
call is sometimes queued on the main thread after the window close command, and will execute after the window has closed, and everything goes up in flames.
What's the best way to control and communicate with a background thread?