0

I've already searched for the problem, and the only hint was the performSelectorOnMainThread solution. I did that, but the view is not updating while the for loop is running. After the loop the progress value is displayed correctly – but that's not the point of an "Progress" View =)

The for loop is within a controller. I call a method to set the progress value:

CGFloat pr = 0.5; // this value is calculated
[self performSelectorOnMainThread:@selector(loadingProgress:) withObject:[NSNumber numberWithFloat:pr] waitUntilDone:NO];

And this is the loading progress method

-(void)loadingProgress:(NSNumber *)nProgress{
    NSLog(@"Set Progress to: %@", nProgress);
    [[self progress] setProgress:[nProgress floatValue]];
}

I also tried to put the method with the for loop into a background thread by using this at the top of the method:

    if([NSThread isMainThread]) {
        [self performSelectorInBackground:@selector(importData) withObject:nil];
        return;
    }
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];  

Maybe somebody can help me with this.

cem
  • 3,311
  • 1
  • 18
  • 23
  • It's correct to let the loop run in another thread, and the way you seem to update the progress view also seems correct. Two questions: first, does your NSLog show that the correct values are set ? Second, are you sure that nothing else is blocking the main thread ? – DarkDust May 25 '11 at 09:53
  • The NSLog-Messages are put out after the for-loop is done. The values are correct – but too late ;) – I'm not sure, that something else is blocking the main thread, but I can say, that I did nothing but displaying the view :) – cem May 25 '11 at 09:57
  • Funny: The Log Messages are shown WHILE the for loop is running, the update to the UIProgressView is made AFTER the for loop was running. – cem May 25 '11 at 10:08
  • 1
    The fact that you see the NSLog while the loop is running but the UI update afterwards smells like "main thread is blocked". You can easily check in your case: do `[self performSelectorOnMainThread:@selector(loadingProgress:) withObject:[NSNumber numberWithFloat:pr] waitUntilDone:YES];` (that's a `YES` instead of `NO`). If it doesn't come back then your main thread is blocked. The debugger will help you in this case. – DarkDust May 25 '11 at 10:16
  • I don't know why, but after I gave up and worked on something else, the progress updates are working on my device. Voodoo =) – cem May 25 '11 at 10:53

1 Answers1

0
 CGFloat pr = 0.5; // this value is calculated
[self performSelectorInBackground:@selector(loadingProgress:) withObject:[NSNumber numberWithFloat:pr]];

try this to set the progress value

Satheesh
  • 361
  • 2
  • 6
  • 17
  • UI updates has to be done on the main thread. If you want to call the method "loadingProgress" on the background you must embed the setProgress in a block executed on the main thread which is meaningless. – foOg Jun 26 '13 at 14:05