Skip to content

Instantly share code, notes, and snippets.

@0xced
Last active October 5, 2016 18:52
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 0xced/2c961f6d149143efd2f760b1e3bcd03a to your computer and use it in GitHub Desktop.
Save 0xced/2c961f6d149143efd2f760b1e3bcd03a to your computer and use it in GitHub Desktop.
Demonstrates how NSProgress handlers being invoked on any queue (as documented) might be problematic
#import <Foundation/Foundation.h>
int main(int argc, char *argv[])
{
@autoreleasepool
{
NSProgress *progress = [[NSProgress alloc] initWithParent:nil userInfo:nil];
progress.cancellationHandler = ^{
NSLog(@"cancel");
};
progress.resumingHandler = ^{
NSLog(@"resume");
};
for (int i = 0; i < 10; i++)
{
NSLog(@"- %2d -", i);
[progress resume];
[progress cancel];
[progress resume];
[progress cancel];
NSLog(@"- %2d -", i);
}
dispatch_main();
}
}
@0xced
Copy link
Author

0xced commented Oct 5, 2016

Sample output on El Capitan 10.11.6 (15G1004):

2016-10-05 20:43:47.185 Untitled[8535:8808039] -  0 -
2016-10-05 20:43:47.187 Untitled[8535:8808039] -  0 -
2016-10-05 20:43:47.187 Untitled[8535:8808078] resume
2016-10-05 20:43:47.187 Untitled[8535:8808039] -  1 -
2016-10-05 20:43:47.187 Untitled[8535:8808079] cancel
2016-10-05 20:43:47.187 Untitled[8535:8808078] cancel
2016-10-05 20:43:47.187 Untitled[8535:8808080] resume
2016-10-05 20:43:47.187 Untitled[8535:8808039] -  1 -
2016-10-05 20:43:47.187 Untitled[8535:8808081] resume
2016-10-05 20:43:47.187 Untitled[8535:8808079] cancel
2016-10-05 20:43:47.187 Untitled[8535:8808078] resume
2016-10-05 20:43:47.187 Untitled[8535:8808080] cancel
2016-10-05 20:43:47.187 Untitled[8535:8808039] -  2 -
2016-10-05 20:43:47.187 Untitled[8535:8808039] -  2 -
2016-10-05 20:43:47.187 Untitled[8535:8808080] resume
2016-10-05 20:43:47.187 Untitled[8535:8808078] cancel
2016-10-05 20:43:47.187 Untitled[8535:8808079] resume
2016-10-05 20:43:47.187 Untitled[8535:8808039] -  3 -
2016-10-05 20:43:47.187 Untitled[8535:8808080] cancel
2016-10-05 20:43:47.187 Untitled[8535:8808039] -  3 -
2016-10-05 20:43:47.187 Untitled[8535:8808080] resume
2016-10-05 20:43:47.187 Untitled[8535:8808079] cancel
2016-10-05 20:43:47.187 Untitled[8535:8808081] resume
2016-10-05 20:43:47.187 Untitled[8535:8808039] -  4 -
2016-10-05 20:43:47.187 Untitled[8535:8808080] cancel
2016-10-05 20:43:47.187 Untitled[8535:8808080] resume
2016-10-05 20:43:47.187 Untitled[8535:8808039] -  4 -
2016-10-05 20:43:47.187 Untitled[8535:8808081] cancel
2016-10-05 20:43:47.187 Untitled[8535:8808079] cancel
2016-10-05 20:43:47.187 Untitled[8535:8808078] resume
2016-10-05 20:43:47.187 Untitled[8535:8808039] -  5 -
2016-10-05 20:43:47.187 Untitled[8535:8808039] -  5 -
2016-10-05 20:43:47.187 Untitled[8535:8808078] resume
2016-10-05 20:43:47.188 Untitled[8535:8808039] -  6 -
2016-10-05 20:43:47.188 Untitled[8535:8808079] cancel
2016-10-05 20:43:47.188 Untitled[8535:8808078] resume
2016-10-05 20:43:47.188 Untitled[8535:8808081] cancel
2016-10-05 20:43:47.188 Untitled[8535:8808039] -  6 -
2016-10-05 20:43:47.188 Untitled[8535:8808080] resume
2016-10-05 20:43:47.188 Untitled[8535:8808079] resume
2016-10-05 20:43:47.188 Untitled[8535:8808078] cancel
2016-10-05 20:43:47.188 Untitled[8535:8808082] cancel
2016-10-05 20:43:47.188 Untitled[8535:8808039] -  7 -
2016-10-05 20:43:47.188 Untitled[8535:8808039] -  7 -
2016-10-05 20:43:47.188 Untitled[8535:8808082] resume
2016-10-05 20:43:47.188 Untitled[8535:8808078] cancel
2016-10-05 20:43:47.188 Untitled[8535:8808079] resume
2016-10-05 20:43:47.188 Untitled[8535:8808080] cancel
2016-10-05 20:43:47.188 Untitled[8535:8808039] -  8 -
2016-10-05 20:43:47.188 Untitled[8535:8808039] -  8 -
2016-10-05 20:43:47.188 Untitled[8535:8808080] resume
2016-10-05 20:43:47.188 Untitled[8535:8808079] cancel
2016-10-05 20:43:47.188 Untitled[8535:8808078] resume
2016-10-05 20:43:47.188 Untitled[8535:8808039] -  9 -
2016-10-05 20:43:47.188 Untitled[8535:8808082] cancel
2016-10-05 20:43:47.188 Untitled[8535:8808082] resume
2016-10-05 20:43:47.188 Untitled[8535:8808039] -  9 -
2016-10-05 20:43:47.188 Untitled[8535:8808078] cancel
2016-10-05 20:43:47.188 Untitled[8535:8808079] resume
2016-10-05 20:43:47.188 Untitled[8535:8808082] cancel

This behavior is documented, but welp.

The cancellation handler may be invoked on any queue.
The pausing handler may be invoked on any queue.
The resuming handler may be invoked on any queue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment