GuybrushThreepwood
GuybrushThreepwood

Reputation: 5616

Updating UIProgressView using GCD - Correct Timing - iPhone

I have a UIProgress view which I want to update as an NSXML Parser runs. The NSXML parser is running on the main thread and takes about 30 seconds to run.

At the end of a parsing section I call a method in my app delegate :

[appDelegate updateLoadingScreen:0.75];

The app delegate then calls the modelLoadingProgressUpdate method in the loadingScreen :

- (void)updateLoadingScreen : (float)progress {
NSLog(@"PROGRESS REQUEST !!!");

NSNumber * progressNS = [NSNumber numberWithFloat:progress];

[loadingScreen modelLoadingProgressUpdate : progressNS];

}

The modelLoadingProgressUpdate method then uses a dispatch queue to update the UIProgressBar :

- (void) modelLoadingProgressUpdate :(NSNumber *)progress {

dispatch_queue_t mainqueue = dispatch_get_main_queue();
dispatch_async(mainqueue, ^  { 

float updateProgress = [progress floatValue];
NSLog(@"Update Progress is %f", updateProgress);

progressView.progress = updateProgress;

});

}

However nothing is updating on my UIProgressView. The modelLoadingProgressUpdate seems to only trigger once the parser has completed.

Can anyone spot what is wrong ?

Upvotes: 0

Views: 728

Answers (1)

yuji
yuji

Reputation: 16725

Here's your problem:

The NSXML parser is running on the main thread and takes about 30 seconds to run.

So your NSXMLParser is blocking the main thread, so the block that you dispatch isn't being executed until the NSXMLParser is done.

Since the NSXMLParser is running on the main thread anyway, you could just get rid of the dispatch_async and just update progressView.progress directly in modelLoadingProgressUpdate:.

Or, you can run the NSXMLParser on a background thread, in which case it won't block the main thread and the progress updating block will run soon after you dispatch it.

Upvotes: 2

Related Questions