3

I have a UIProgressView added to self.view as subview. I have a UITableView with rows loaded. I need image in every row but I don't want the app to wait for all of them, so I decide to run NSThread with loading images process. When I try to update progress of the UIProgressView from inside my thread - it doesn't get updated. Do I need to implement some delegates maybe?

Initialization

    progressView = [[[UIProgressView alloc] initWithFrame:CGRectZero] autorelease];
    [progressView setFrame:CGRectOffset(CGRectMake(0, 0, 320, 8), 0, 1)];
    [self.view addSubview:progressView];

Then I run my thread

    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    [progressView setProgress:0.0];
    NSThread *myThread = [[NSThread alloc] initWithTarget:self
                                                 selector:@selector(imageLazyLoading)
                                                   object:nil];

    [myThread start];
    [pool release];
    [myThread release];

Then I try to update it

CGFloat pr;
for(int i=0; i < [self.itemsToDisplay count]; i++){
    if (!runThread) {
        return;
    }
    pr = (CGFloat)i/(CGFloat)[self.itemsToDisplay count]; 
    [self performSelectorOnMainThread:@selector(updateProgressBar:)
                           withObject: [NSNumber numberWithFloat:pr]
                        waitUntilDone: YES];
    progressView.progress += 0.5;.............

and nothing at all....

Artem Svystun
  • 179
  • 1
  • 14
  • You sure pr is what u expect it to be?? – Daniel Sep 29 '10 at 14:57
  • What does `updateProgressBar:` look like? I wouldn't recommend changing progressView.progress anywhere but the main thread. – Kris Markel Sep 29 '10 at 18:19
  • You must update the UI on the main thread, not on a secondary one. – Nyx0uf Sep 29 '10 at 18:25
  • He's using `performSelectorOnMainThread:`, so the thread issue *is* covered. – pgb Sep 29 '10 at 22:31
  • updateProgressBar looks like - (void) updateProgressBar:(NSNumber*)num { [progressView setProgress:[num floatValue]]; } – Artem Svystun Sep 30 '10 at 06:34
  • It is strange... I deleted all about progressView in my code. And rewrite everything from scratch. And guess what - it is working now. The line [self performSelectorOnMainThread:@selector(updateProgressBar:) withObject: [NSNumber numberWithFloat:pr] waitUntilDone: YES]; works and the line progressView.progress += 0.5; doesn't. Anyway thanks for your time and I appreciate it. – Artem Svystun Sep 30 '10 at 06:37

2 Answers2

0

All you have to do is , update the Progressview on Main Thread. Here is the article about it. http://www.xprogress.com/post-36-threading-tutorial-using-nsthread-in-iphone-sdk-objective-c/

iNeal
  • 1,729
  • 15
  • 23
0

I had the same problem with iOS 5.0. Seems that up until 5.0, you could update the 'progress' variable from any thread, but the setNeedsDisplay must be called from the main thread. From 5.0 and onwards, you must set the 'progress' value from the main thread as well. Hope it helps anybody.

gilm
  • 7,690
  • 3
  • 41
  • 41