I have an issue with threading in Cocoa Touch. I call a collection class initializer from my MainViewController
directly like this:
[flickrImages initWithBoundingBox:currentBoundingBox];
This collection has a progress property which is observed by the MainViewController. This posts floats between 0 and 1 to drive an UIProgressView
. Although the populating of that image collection all happens using asynchronous requests, the UIProgressView
does not get updated until the very end. Debugging shows that the progress updates are received timely. My original code for the KVO observer was:
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
if([keyPath isEqual:@"progress"]) {
[self performSelector:@selector(updateProgressViewWithProgress:) withObject:[change valueForKey:NSKeyValueChangeNewKey] afterDelay:0.0];
}
}
That made all the updateProgressViewWithProgress:
calls happen toward the end of execution.
Next I tried this:
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
if([keyPath isEqual:@"progress"]) {
[self performSelectorOnMainThread:@selector(updateProgressViewWithProgress:) withObject:[change valueForKey:NSKeyValueChangeNewKey] waitUntilDone:YES];
}
}
Now all the updateProgressViewWithProgress:
calls are nicely interspersed with the rest of the logging output - even though I have not consciously departed from the main thread. Still, the UIProgressView
does not update until the end and skips to 1.0 directly. What else should I be doing to get this right?