Last active
March 26, 2016 16:21
-
-
Save robertmryan/f5a873be1763f246e76c to your computer and use it in GitHub Desktop.
dispatch_async works as expected
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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]); | |
}); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Pursuant to this Stack Overflow question,
dispatch_async
works as expected (as illustrated by the above); onlydispatch_after
manifests the curious behavior you see in that question.