johnbakers
johnbakers

Reputation: 24771

Real-time progress bar updates

When updating a progress bar or other progress indicator in real-time to reflect activity going on in an app's processing, I see no way to do this without using this clever code snippet:

[[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantPast]];

Otherwise, the progress bar waits until the run loop finishes, in which case it then jumps to the progress bar's conclusion.

If you want to update progress during a single run loop (or what I perceive to be a single run loop), is this the only way to do so?

How are progress bars typically coded to update in real-time?

Is the above code dangerous? I really don't know much about direct manipulation of the run loop, and it makes me nervous to do something in an app that I don't understand. This code works, but is it a good idea?

Upvotes: 1

Views: 520

Answers (2)

iremk
iremk

Reputation: 677

i am using NSTimer for this kind of need. let's say you are going to play/stream an audio file and you need to show duration with the progress bar's position so this is my way.

hope this helps..

Upvotes: 1

Stephen Darlington
Stephen Darlington

Reputation: 52565

Run your lengthy operation on a background thread/NSOperationQueue/GCD and update the UI periodically. The UI always runs on the main thread so the update will to effect quickly. Try something like this:

for (...) {
  // some process
  // ...

  dispatch_async(dispatch_get_main_queue(),^ {
    [self.progressIndicator setProgress:x animated:YES];
  });
}

In general you don't want to run long or complex operations on the main thread as it will block the whole UI.

Upvotes: 3

Related Questions