Created
August 7, 2013 15:00
-
-
Save ShonFrazier/6174817 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
void dispatch_wait_while(dispatch_wait_condition_t condition_block, dispatch_time_t interval, dispatch_wait_completion_t completion_block) { | |
if ( !condition_block ) return; | |
if ( !interval ) return; | |
if ( !completion_block ) return; | |
dispatch_source_t timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, dispatch_get_current_queue()); | |
dispatch_source_set_timer(timer, DISPATCH_TIME_NOW, interval, 0.25*NSEC_PER_SEC); | |
dispatch_source_set_event_handler(timer, ^{ | |
if (!condition_block()) { | |
dispatch_source_cancel(timer); | |
completion_block(); | |
} | |
}); | |
dispatch_resume(timer); | |
} | |
/*Example*/ | |
dispatch_wait_while(^bool { | |
return SomeTypeGetStatus(anObjectOfSomeType) == StatusNotReady; | |
}, | |
0.5 *NSEC_PER_SEC, | |
^{ | |
OperateOnSomeType(anObjectOfSomeType); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment