1

I have 2 windows in my cocoa app. Main window opens a sub window. On click of OK on the sub window, I invoke a deligate on main form which will tell that OK button is clicked on the sub window.

Now, I need to run a long running process on the main window "in the background" so that the window will not become unresponsive. I also have progress bar which should show progress of this long running process.

Please let me know, what is the best way to achieve this.

2 Answers 2

4

You should start with Apple's Concurrency Programming Guide.

Specially the section about NSOperationQueue.

4

You can use Grand Central Dispatch for this. First you create a dispatch queue which will contain operations you want to perform on another thread. Each operation is represented as a Objective-C block (closure).

First you get a queue to put the task you want to run on another thread.

dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);

Then you place a block representing the work you want to do on this queue:

dispatch_async(queue, ^{
    // this happens on separate thread
    NSImage *image = produceImageFromSomeReallySlowOperation()

    dispatch_async(dispatch_get_main_queue(), ^{
        // this happens on main thread
        [myView setImage:image];
    });

});

The dispatch_get_main_queue() function returns the queue which is used for operations on the main thread (where the GUI is executed). This means that [myView setImage:image] will be executed on the main thread. You can place your update of the progress bar here. Just dispatch on the main queue to update the progress at every point in your algorithm where it makes sense to do so.

All of this can also be performed with NSOperation which provides a higher level Objective-C interface to the same functionality. But using GCD directly is sometimes easier. It depends on what you want to do.

4
  • 1
    Apple recommends you always use the highest level abstraction of an API unless you need specific behaviour offered only by the lower level APIs. So you should really recommend NSOperationQueue
    – Tim
    Feb 14, 2014 at 12:17
  • 1
    @Jeff: NSOperationQueue is a neat class, but GCD is equally versatile as well and I think it is also easier to jump in to. NSOperationQueue takes some practice and sometimes pulled hair in my experience for new users.
    – SevenBits
    Feb 14, 2014 at 12:46
  • @SevenBits interesting you have the same experience. I also find NSOperation harder to reason about. Despite NSOperation being higher level it is older, so I think it makes sense that GCD is better designed because it is a newer API, with the benefit of hindsight. Feb 14, 2014 at 12:51
  • @AdamSmith Otherwise, both APIs are nice to use. I personally use GCD more so I don't have to subclass NSOperation but both let you do things like which thread you want to run on, etc. There are pros and cons of each.
    – SevenBits
    Feb 14, 2014 at 12:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.