Skip to content

Instantly share code, notes, and snippets.

@yasirmturk
Created July 8, 2014 07:54
Show Gist options
  • Save yasirmturk/76cb724657b0105b0a38 to your computer and use it in GitHub Desktop.
Save yasirmturk/76cb724657b0105b0a38 to your computer and use it in GitHub Desktop.
Avoid multiple Executions of a method at any given time
dispatch_semaphore_t semaphore = dispatch_semaphore_create(1);
dispatch_queue_t renderQueue = dispatch_queue_create("com.throttling.queue", NULL);
- (void) onlyExecuteOnceAtATime {
if (dispatch_semaphore_wait(semaphore, DISPATCH_TIME_NOW) == 0) {
dispatch_async(renderQueue, ^{
// execution code goes here
dispatch_semaphore_signal(semaphore);
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment