View Full Version : UIProgressView not updated
Nov 8, 2008, 11:13 AM
In my app I respond to a button event by doing some work that could take several seconds, so I am using a UIProgressView to show progress. The problem is that even though I am setting the progress property the UI does not update. I am wondering if this is because I am doing all this in my IBAction method which is called from the button event -- is the problem that the UI will not get updated by changes I make to UIProgressView.property until this method returns? If so, do I then need to do my work in a separate thread, and have the IBAction method launch this thread and then return immediately?
Nov 10, 2008, 08:56 AM
Just a follow up on this....It turns out my problem is that I was using NSURLConnection to load a url, and even though I was doing it asynchronously it was running in the main thread. My error was that I incorrectly assumed that asynchronous loading of urls happened in a new background thread, which is not the case. So even though I was periodically setting the progress property of the control from within that thread, none of those updates were processed until I finished parsing the entire data set since I was doing this in the main thread. That led to the user seeing the progress view "stuck" at 0% for the entire duration of the task, and then seeing it jump to 100% once the task was done.
To get the UI to display properly I changed my code to load the url in a background thread:
[self performSelectorInBackground:@selector(loadUrl:) withObject:url];
Then in the background thread when I wanted to update the UI I had to trigger a method call in the main thread to do the updating:
NSNumber* f = [NSNumber numberWithFloat:(float) (++nTasksComplete) / (float) nTasksTotal] ;
[self performSelectorOnMainThread:@selector(updateProgressView:) withObject:f waitUntilDone:NO];
Then everything worked fine, and my progress view control shows the progress as I parse through the downloaded data
Jul 31, 2009, 05:21 AM
you solve my problem, thanks.:D