Last active
August 29, 2015 13:57
-
-
Save matteogobbi/9716215 to your computer and use it in GitHub Desktop.
Block execution by a flag of blocks queued with dispatch_after.
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
//Flag | |
_isCancelled = @(NO); | |
//Create a queue | |
dispatch_queue_t queue = dispatch_queue_create("queue", NULL); | |
//Insert 3.000 blocks executed with a delay of 1 sec | |
for (int i=0; i<3000; i++) { | |
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1* NSEC_PER_SEC)), queue, ^(void){ | |
if(![_isCancelled boolValue]) | |
NSLog(@"Block #%d executed", i); | |
else | |
NSLog(@"Block #%d deleted", i); | |
}); | |
} | |
//After 2 secs set the flag to doesn't execute the other blocks | |
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2* NSEC_PER_SEC)), dispatch_get_main_queue(), ^(void){ | |
NSLog(@"DELETE BLOCKS FROM NOW"); | |
_isCancelled = @(YES); | |
}); | |
/* | |
2014-03-22 23:43:33.891 test[2415:1303] Block #0 executed | |
2014-03-22 23:43:33.892 test[2415:1303] Block #1 executed | |
2014-03-22 23:43:33.893 test[2415:1303] Block #2 executed | |
2014-03-22 23:43:33.893 test[2415:1303] Block #3 executed | |
2014-03-22 23:43:33.893 test[2415:1303] Block #4 executed | |
............. | |
2014-03-22 23:43:34.889 test[2415:1303] Block #2380 executed | |
2014-03-22 23:43:34.889 test[2415:1303] Block #2381 executed | |
2014-03-22 23:43:34.890 test[2415:1303] Block #2382 executed | |
2014-03-22 23:43:34.890 test[2415:1303] Block #2383 executed | |
2014-03-22 23:43:34.890 test[2415:70b] DELETE BLOCKS FROM NOW | |
2014-03-22 23:43:34.890 test[2415:1303] Block #2384 executed | |
2014-03-22 23:43:34.890 test[2415:1303] Block #2385 deleted | |
2014-03-22 23:43:34.890 test[2415:1303] Block #2386 deleted | |
2014-03-22 23:43:34.891 test[2415:1303] Block #2387 deleted | |
2014-03-22 23:43:34.891 test[2415:1303] Block #2388 deleted | |
2014-03-22 23:43:34.891 test[2415:1303] Block #2389 deleted | |
............. | |
2014-03-22 23:43:35.153 test[2415:1303] Block #2998 deleted | |
2014-03-22 23:43:35.153 test[2415:1303] Block #2999 deleted | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment