Skip to content

Instantly share code, notes, and snippets.

@robertmryan
Last active March 26, 2016 16:21
Show Gist options
  • Save robertmryan/f5a873be1763f246e76c to your computer and use it in GitHub Desktop.
Save robertmryan/f5a873be1763f246e76c to your computer and use it in GitHub Desktop.
dispatch_async works as expected
// this behaves as expected
- (void)exampleOne {
dispatch_async(queue, ^{
[NSThread sleepForTimeInterval:10];
NSLog(@"10s --- %@", [NSThread currentThread]);
});
dispatch_async(queue, ^{
[NSThread sleepForTimeInterval:5];
NSLog(@"5s --- %@", [NSThread currentThread]);
});
dispatch_async(queue, ^{
[NSThread sleepForTimeInterval:5];
NSLog(@"5s --- %@", [NSThread currentThread]);
});
dispatch_async(queue, ^{
[NSThread sleepForTimeInterval:5];
NSLog(@"5s --- %@", [NSThread currentThread]);
});
}
// so does this
- (void)exampleTwo {
dispatch_async(queue, ^{
[NSThread sleepForTimeInterval:10];
NSLog(@"10s --- %@", [NSThread currentThread]);
});
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
dispatch_async(queue, ^{
[NSThread sleepForTimeInterval:5];
NSLog(@"5s --- %@", [NSThread currentThread]);
});
dispatch_async(queue, ^{
[NSThread sleepForTimeInterval:5];
NSLog(@"5s --- %@", [NSThread currentThread]);
});
dispatch_async(queue, ^{
[NSThread sleepForTimeInterval:5];
NSLog(@"5s --- %@", [NSThread currentThread]);
});
});
}
@robertmryan
Copy link
Author

Pursuant to this Stack Overflow question, dispatch_async works as expected (as illustrated by the above); only dispatch_after manifests the curious behavior you see in that question.

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