Skip to content

Commit

Permalink
Use NSProgress cancelation
Browse files Browse the repository at this point in the history
  • Loading branch information
matej committed Jul 17, 2016
1 parent b11d70c commit 1f794f6
Showing 1 changed file with 8 additions and 5 deletions.
13 changes: 8 additions & 5 deletions Demo/Classes/MBHudDemoViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -151,11 +151,15 @@ - (void)determinateNSProgressExample {
// Set the determinate mode to show task progress.
hud.mode = MBProgressHUDModeDeterminate;
hud.label.text = NSLocalizedString(@"Loading...", @"HUD loading title");


// Set up NSPorgress
NSProgress *progressObject = [NSProgress progressWithTotalUnitCount:100];

hud.progressObject = progressObject;


// Configure a cacnel button.
[hud.button setTitle:NSLocalizedString(@"Cancel", @"HUD cancel button title") forState:UIControlStateNormal];
[hud.button addTarget:progressObject action:@selector(cancel) forControlEvents:UIControlEventTouchUpInside];

dispatch_async(dispatch_get_global_queue(QOS_CLASS_USER_INITIATED, 0), ^{
// Do something useful in the background and update the HUD periodically.
[self doSomeWorkWithProgressObject:progressObject];
Expand Down Expand Up @@ -311,10 +315,9 @@ - (void)doSomeWork {
}

- (void)doSomeWorkWithProgressObject:(NSProgress *)progressObject {
self.canceled = NO;
// This just increases the progress indicator in a loop.
while (progressObject.fractionCompleted < 1.0f) {
if (self.canceled) break;
if (progressObject.isCancelled) break;
[progressObject becomeCurrentWithPendingUnitCount:1];
[progressObject resignCurrent];
usleep(50000);
Expand Down

0 comments on commit 1f794f6

Please sign in to comment.