I have to do a IO operation which might take long to complete. The IO oeration is invoked from a button.
Ofcourse the UI hangs untill the operation completes. So I suppose I need to do IO on some background thread, but when the operation completes I have to update window’s labels to signal the operation complete. Which I suppose I should be doing on the main thread (like EDT in java and similar in C#) , correct ?
in C# there is class something like TaskAsync and similar in Java android. Which lets you complete the long task in another thread and the when the task is complete a handler is called on main thread , so that UI can be updated on main thread,
What exactly does cocoa has to do similar task, that is allow a long operation on seperate thread than main and then somehow facilitate to update the userinterface on main thread.
Assuming you have a handle to the view controller where you want to update the UI, you could use the NSObject “
performSelectorOnMainThread:withObject:waitUntilDone:” method.2) you could also utilize Grand Central Dispatch with code like this:
3)
Or you could even post an NSNotification from your background thread where the observer (in the main thread) could update the UI.
You might find some additional useful information in this related question.