I have a method with a for() loop. In that loop, mylabel.text is updated many times. However, the actual label does not update on the screen until the method is done, updating with the last value created in the for() loop.
Using an NSLog, which does update in the middle of the for() loop, I see the value indeed changing for the label many times.
Is it the general practice in iOS to not update labels in the middle of the for() loop? I would imagine there is a way to do this.
You can make the UI update by telling the run loop to run like this:
for (NSInteger i = 0; i < 10; i++) {
[label setText:...];
[[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantPast]];
}
From my earlier comment:
Note that this (
runMode:beforeDate:
) can have all kinds of bizarre side-effects. When you call runMode:beforeDate:, all kinds of things could happen in the middle of your loop. Timers could fire; WebKit can do all kinds of madness; delayed selectors can fire. This is a very dangerous trick. Sometimes useful, occasionally necessary (especially on Mac), but not a general-purpose tool.
The better solution is to schedule your updates on the main dispatch queue:
for (NSInteger i = 0; i < 10; i++) {
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, i * NSEC_PER_SEC),
dispatch_get_main_queue(), ^{
[self.label setText:[NSString stringWithFormat:@"%d", i]];
});
}
This schedules 10 updates 1 second apart. It can be adapted to all kinds of other requirements without creating a blocking method on the main run loop.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With