Question

Possible Duplicate:
NSProgressIndicator progress with For loops?

I have a simple question this time (hopefully). I have an NSProgressIndicatorView (progress bar) and I want to update its value and a status label as a for loop runs:

for(int i=0; i<[nameArr count]; i++)
{
    NSString* str = [nameArr objectAtIndex:i];
    [text setStringValue:str];
    [status setStringValue:[NSString stringWithFormat:@"Processing names...%d of %ld",(i+1),[nameArr count]]];
    [progress setDoubleValue:i];
}

This code works, but the status label and the progress bar do not update until the entire for loop has finished. I can watch things work as it runs if I send them as an NSLog, so I assume there should be an easy way (without creating an NSOperation) to similarly update my status label and progress bar as I go.

Any ideas?

Was it helpful?

Solution

After doing some digging, the UI can be updated asynchronously through for loops with the following code:

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^(void) {
    for(int i=0; i<max; i++)
    {
        //Run for loop stuff here

        dispatch_async(dispatch_get_main_queue(), ^(void) {
           //Run UI updates in here 
        });
    }
});
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top