Skip to content

Instantly share code, notes, and snippets.

@craigmarvelley

craigmarvelley/progress.m

Last active Apr 26, 2017
Embed
What would you like to do?
NSProgress/UIProgressView example
// Create a progress bar view
UIProgressView *progressView = [[UIProgressView alloc] initWithProgressViewStyle:UIProgressViewStyleBar];
progressView.progress = 0;
// Assume we have a progress object
NSProgress *progress = [self progressForOngoingTask];
// Observe the 'fractionCompleted' property and update the progress view as it changes
[self.KVOController observe:progress keyPath:@"fractionCompleted" options:NSKeyValueObservingOptionNew block:^(id _Nullable observer, id _Nonnull object, NSDictionary<NSString *, id> *_Nonnull change) {
dispatch_async(dispatch_get_main_queue(), ^{
progressView.progress = progress.fractionCompleted;
});
}];
// Observe a child NSProgress object's userInfo property and update the parent's userInfo as it changes
[self.KVOController observe:childProgress keyPath:@"userInfo" options:NSKeyValueObservingOptionNew block:^(id _Nullable observer, NSProgress * _Nonnull childProgress, NSDictionary<NSString *, id> *_Nonnull change) {
[parentProgress setUserInfoObject:childProgress.userInfo[@"notesDownloaded"] forKey:@"notesDownloaded"];
[parentProgress setUserInfoObject:childProgress.userInfo[@"totalNotesToDownload"] forKey:@"totalNotesToDownload"];
}];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.