setProgress is no longer updating UIProgressView since iOS 5
Asked Answered
J

2

9

I have a little trouble with a progress bar since iOS 5 came out. The code below was working fine before iOS 5 but with iOS 5 the progress bar is no longer displaying the new progress that is set within a loop.

The code is expected to work like this:

  1. Create the progress bar (works)
  2. In a new background process: Set an initial progress of 0.25 (works)
  3. In the same background process: Update the progress while going thru the loop (worked in iOS 4)

Here's the code for the bar init:

// create a progress bar
UIProgressView *progressBar = [[UIProgressView alloc] initWithFrame:CGRectMake(coverSizeX*0.25, coverSizeY - 34.0, coverSizeX*0.5, 9.0)];
progressBar.progress = 0.0;
progressBar.progressViewStyle = UIProgressViewStyleBar;

and in a different thread it sets a starting point for the progress to 0.25:

// set an initial progress
[progressBar setProgress: 0.25];

a little later it is updating the progress within a loop to display the download progress:

// within a for-loop:
NSNumber *counterPercentage;
for ( pageDownload = 1; pageDownload < pagesToDownload; pageDownload++ ) {
    counterPercentage = [[NSNumber alloc] initWithFloat: (float)pageDownload / (float)((float)pagesToDownload)];
    [progressBar setProgress: [counterPercentage floatValue]];
    [progressBar performSelectorOnMainThread:@selector(setNeedsDisplay) withObject:nil waitUntilDone:YES];
}

… but the progress is not shown on the screen, the progress bar is stuck at the initial 0.25 progress that was set.

Were there any changes with the iOS 5 release that could have broken it?

Jacquelinejacquelyn answered 10/1, 2012 at 12:22 Comment(0)
G
18

I've seen a lot of questions like this one since the iOS 5 switch, and I'm not sure why there is a problem only in iOS 5. But mainly because I'm not sure why there wasn't a problem before.

In your code you call [progressBar setProgress: [counterPercentage floatValue]]; from a background thread. This is a UI call and should not be made from a background thread. Also you call setNeedsDisplay which is not necessary to update the progressBar since an UIProgressView knows how to display itself. iOS 5 seems to have made the requirements for updating the UI more stringent, but only to the point of what are best practices anyway.

To my eye this looks like a perfect use for blocks. Using blocks your for loop could be written this way:

for ( pageDownload = 1; pageDownload < pagesToDownload; pageDownload++ ) {
        // Other stuff in background
    dispatch_async(dispatch_get_main_queue(), ^{
        progressBar.progress = ((float)pageDownload/(float)pagesToDownload);
    });
        // Other stuff in backgroud
}
Galantine answered 10/1, 2012 at 13:51 Comment(1)
Thank you, this worked! Also thank you for explaining a little more than just providing a working solution :-)Jacquelinejacquelyn
E
2

It does work in iOS 5 and the easiest way to to it is here:

.h file:

IBOutlet UIProgressView *WhateverYouWantToCallIt;

.m file:

[WhateverYouWantToCallIt setProgress:(float) 0.3];

And where it says 0.3, you can put whatever value you like (within 0 to 1)

Enolaenormity answered 1/11, 2012 at 11:0 Comment(1)
@ user1791131 it doesn't work in iOS 5. The main issue is that he was making a UI change from a thread other than the main thread. Any changes to UI made from any thread that is not the main thread will not post.Cris

© 2022 - 2024 — McMap. All rights reserved.