Reputation: 1316
I know this question asked many times on StackOverflow, But I already checked that. my problem is I am updating progress bar like this on main thread after getting response from api hit
dispatch_async(dispatch_get_main_queue(),^{
for (int i = 0; i < 5000; i++)
{
NSLog(@"%f",i);
[progressView setProgress:i/5000 animated:YES];
}
});
from this way My progressivew not updating but when I set static digit it's working
[progressView setProgress:0.7 animated:YES];
After searching I found that there is issue regarding float decimal number digits. Because i/5000 give 0.000000 always even on 4999/5000. So after some googling I chnaged
dispatch_async(dispatch_get_main_queue(),^{
for (int i = 0; i < 5000; i++)
{
float f = (float)i/5000;
[progressView setProgress:f animated:YES];
}
});
now it's updating but progress view not update continuously, it's track update in last at once from 0 to 1
Upvotes: 0
Views: 313
Reputation: 1316
@trapper you are right, thanks for help me but I got this solution before a min ago, no problem. Here I am posting my answer to help another guys.
This was a thread issue. The drawing of progress are stacked in the main queue and executed only at some point, at the end.
So the solution is to run the loop in a user thread, and call the main thread when you need to draw (that is compulsory). So, change update progress as follows
dispatch_async(dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^(void){
//Background Thread
for (int i = 0; i < 50000; i++) {
float f = (float)i/50000;;
dispatch_async(dispatch_get_main_queue(), ^(void){
//Run UI Updates
[self->progressView setProgress:f animated:YES];
});
}
});
Upvotes: 0
Reputation: 12003
The first issue you got right, the integer division is always going to give you 0.
The second issue is that the UI updates on the main thread - and only periodically even then. Now your code is also running on the main thread, so the UI is completely blocked while your code executes. It will only get a chance to update after your code ends, so you will only ever see the final value actually get rendered.
Upvotes: 1