Last active
August 29, 2015 14:10
-
-
Save zakol/1091a813efd817eb8b1a to your computer and use it in GitHub Desktop.
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
+ (dispatch_block_t)performBlock:(void (^)(dispatch_block_t safeBlock))block | |
{ | |
dispatch_block_t strongBlock; | |
__weak __block dispatch_block_t weakBlock = strongBlock = ^{ | |
block(weakBlock); | |
}; | |
strongBlock(); | |
return strongBlock; | |
} | |
+ (void)test | |
{ | |
// Config | |
const float actionFrequency = 1.0; | |
const float checkFrequency = 5.0; | |
const int iterationsNumber = 5; | |
// Scheduler | |
static int iteration = 0; | |
dispatch_block_t strongBlock = [self.class performBlock:^(dispatch_block_t safeBlock) { | |
NSLog(@"Action: %d/%d", ++iteration, iterationsNumber); | |
if (iteration == iterationsNumber) return; | |
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(NSEC_PER_SEC / actionFrequency)), dispatch_get_main_queue(), safeBlock); | |
}]; | |
// Watchdog | |
__weak __block dispatch_block_t weakBlock = strongBlock; | |
[self.class performBlock:^(dispatch_block_t safeBlock) { | |
NSLog(@"Check: %@", weakBlock ? @"RETAINED" : @"RELEASED"); | |
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(NSEC_PER_SEC / checkFrequency)), dispatch_get_main_queue(), safeBlock); | |
}]; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment