I want something similar to C#'s BackgroundWorker
.
I want to execute a block code of code (irrespective of what kind of code it is. It could be networking, I/O, complex math operations, whatever) in the background while I animate a NSProgressIndicator
. Once the operation is complete, not only do I want to hide the progress indicator but I also want to receive result object(s) from the background code back to the main code.
What is the best way to do this?
Thanks for the help!
You may want to do some reading about NSOperation, NSOperationQueue, and blocks.
Using these you can execute your "block" of code asynchronously and have it "schedule" an update of the progress indicator on the main thread every so often. You can set a completion block (callback) to execute when the operation is complete and it can call a function to be executed on the main thread.
This article provides a nice overview of the options with small examples.
This answer provides an example for defining and using a completion block.