Skip to content

Instantly share code, notes, and snippets.

@craigmarvelley
Last active April 26, 2017 08:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save craigmarvelley/6ccb7933c66e83fc5c721f7e2bff3d4e to your computer and use it in GitHub Desktop.
Save craigmarvelley/6ccb7933c66e83fc5c721f7e2bff3d4e to your computer and use it in GitHub Desktop.
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