Update UIProgressView from NSProgress

I have this observer and I'm 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]; } } } 

NSLog displays 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 progressBar is not updated. I notice that if I use NSTimer, it updates:

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

Why is this?

+3
ios
source share
2 answers
  • Make sure the values ​​are correct.
  • Make sure your KVO is in the main thread.
  • If not, dispatch_async is a block of code that changes the interface in the main queue.
+3
source share

try to execute the 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]; } 
+3
source share

All Articles