2

I am trying to update a UIProgressView progress bar that I have in a UIView during a long resource loading process. Let's say I'm loading a bunch of bitmaps from a NIB file (like maybe a hundred). After I load 10, I issue a .progress to the UIProgressView that is part of a UIView that is already being displayed. So, I issue:

myView.myProgressView.progress=0.2;

Then, I load another 10 bitmaps, and issue:

myView.myProgressView.progress=0.4;

etc., etc. When the app runs, the progress bar doesn't advance. It simply stays at its initial position. At the risk of sounding like a complete moron, do I have to load my resources on a separate thread so the OS can update the UI, or, is there an easier way? Thanks for any assistance.

kspeacock
  • 173
  • 2
  • 9

2 Answers2

1

Yes. Load them on a separate thread. Or just use something like performSelector:

[self performSelector:@selector(setProgressBar) withObject:nil afterDelay:0.0];

(and create a setProgressBar function which reads the current value from a member variable and updates the UI)

marcc
  • 12,295
  • 7
  • 49
  • 59
  • Hi, Marcc, I tried your performSelector approach to no avail. The setProgressBar method got called AFTER all my other code completed. I didn't even try to update the UIProgressView. I simply put an NSLog in setProgressBar and that came out on the console AFTER all the other resource loading methods. Any other suggestions ? Can you point me to a reference for maybe doing all this stuff on another thread ? – kspeacock Aug 07 '09 at 03:36
  • 1
    Oops. So the idea is right, you need to update the data on one thread and not block the main thread (UI thread) while doing it. I think you actually need to use performSelectorOnMainThread if you take this approach (search in the developer site for the docs on this one). – marcc Aug 07 '09 at 04:12
0

You could run a step of the runloop after each update of the UI:

SInt32 result;
do {
    result = CFRunLoopRunInMode(kCFRunLoopDefaultMode, 0, TRUE);
} while(result == kCFRunLoopRunHandledSource);

It could have other bad consequences (for example, enable user to interact with the UI, execute delegates of view controller such as viewDidAppear before they should be executed, etc) so be very, very careful.

Ivan Vučica
  • 9,529
  • 9
  • 60
  • 111