Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Update UIProgressView from NSProgress

Tags:

ios

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??

like image 560
Alessandro Avatar asked May 31 '26 07:05

Alessandro


1 Answers

  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.
like image 89
nscoding Avatar answered Jun 03 '26 02:06

nscoding



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!