3

I have this observer and I am trying to update my UIProgressView in this way:

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
NSProgress *progress = object;
NSLog(@"PROG: %f", progress.fractionCompleted);
[progressBar setProgress:_progressReceive.fractionCompleted animated:YES];

// Check which KVO key change has fired
if ([keyPath isEqualToString:kProgressCancelledKeyPath]) {
    // Notify the delegate that the progress was cancelled
    [progressBar removeFromSuperview];
    NSLog(@"CANCEL");

}
else if ([keyPath isEqualToString:kProgressCompletedUnitCountKeyPath]) {
    // Notify the delegate of our progress change

    if (progress.completedUnitCount == progress.totalUnitCount) {
        // Progress completed, notify delegate
        NSLog(@"COMPLETE");
        [progressBar removeFromSuperview];
    }
}
}

The NSLog is displaying data:

...
2013-10-29 19:55:26.831 Cleverly[2816:6103] PROG: 0.243300
2013-10-29 19:55:26.835 Cleverly[2816:6103] PROG: 0.243340
2013-10-29 19:55:26.838 Cleverly[2816:6103] PROG: 0.243430
...

But the progressBar is not updating. I notice that if I use an NSTimer instead it does update:

-(void)progressSend:(NSTimer*)timer{
NSLog(@"Fraction Complete: %@", [NSNumber numberWithDouble:progressSend.fractionCompleted]);
[progressBar setProgress:progressSend.fractionCompleted animated:YES];
}

Why is this??

Alessandro
  • 4,000
  • 12
  • 63
  • 131
  • 1
    The UIProgressView has to show on a seperate thread. If my understanding of the Timer is correct it is technically running on its own seperate thread. – logixologist Oct 29 '13 at 19:00
  • See if this helps: http://stackoverflow.com/questions/11226509/uiprogressview-not-updating?rq=1 – logixologist Oct 29 '13 at 19:03
  • I am using progress bar to download file. I am updating inside - (void) connection: (NSURLConnection*) connection didReceiveData: (NSData*) data NO Thread needed. – hasan Oct 29 '13 at 19:05

2 Answers2

3
  1. Make sure that the values are right
  2. Make sure that your KVO is on the main thread.
  3. If not, dispatch_async the block of code that is changing UI on the main queue.
nscoding
  • 866
  • 1
  • 6
  • 10
3

try to perform on main thread:

[self performSelectorOnMainThread:@selector(updateProgress:) withObject:[NSArray arrayWithObjects:progressBar, progress.fractionCompleted, nil] waitUntilDone:NO];

Method:

-(void) updateProgress:(NSArray*)array
{
    UIProgressView *progressBar = [array objectAtIndex:0];
    NSNumber *number = [array objectAtIndex:1];
    [progressBar setProgress:number.floatValue animated:YES];
}
hasan
  • 23,815
  • 10
  • 63
  • 101