0

I have math function (about O^(10^7)) and while it searching result I have to display progress. So I have progress bar. The question is, how to update progress bar? I have tried to do it so:

self.progressBar.progress = 0.0;
self->progress = 0.0;
for (1..10^7) {
    ...
    if (self.progressBar.progress < self.progress + 0.01) {
        [self updateProgress];
    }
}
[self updateProgress];
Gabriele Petronella
  • 106,943
  • 21
  • 217
  • 235
Timur Bernikovich
  • 5,660
  • 4
  • 45
  • 58

2 Answers2

2

Run the operation in a separate thread. That's a must.

Can you divide the operation into steps? Do you know the number of steps? Then update progress bar after every step to stepIndex/numSteps percents.

If you can't divide the operation into steps but you know how long the operation will take, you can update the bar to timeSinceStart/expectedTime percents. Use a NSTimer for the periodic updates.

If you can't do that, use only a UIActivityIndicator, that is, a component that shows the application is doing something but showing no indication how long the operation can take.

Sulthan
  • 128,090
  • 22
  • 218
  • 270
1

Sometimes you have to do:

CFRunLoopRunInMode (CFRunLoopCopyCurrentMode(CFRunLoopGetCurrent()), 0, FALSE);

To get the UI to update the progress bar.

Also.. you have animated:YES and that's not available in all versions of iOS.

You might want to consider instead in your updateProgress method:

if ([activityProgressView respondsToSelector:@selector(setProgress:animated:)]) {
    [activityProgressView setProgress:0.5 animated:YES];
}
else {
    [activityProgressView setProgress:0.5];
}
CFRunLoopRunInMode (CFRunLoopCopyCurrentMode(CFRunLoopGetCurrent()), 0, FALSE);
badweasel
  • 2,349
  • 1
  • 19
  • 31