Skip to content

Instantly share code, notes, and snippets.

@swillits
Created March 19, 2012 06:18
Show Gist options
  • Save swillits/2098574 to your computer and use it in GitHub Desktop.
Save swillits/2098574 to your computer and use it in GitHub Desktop.
void __AllowOnceEvery(double seconds, void (^block)(void), NSTimeInterval * lastTime)
{
NSTimeInterval now = [NSDate timeIntervalSinceReferenceDate];
if (now - *lastTime > seconds) {
*lastTime = now;
block();
}
}
#define AllowOnceEvery(seconds, block) \
do {\
static NSTimeInterval lastTime = 0;\
__AllowOnceEvery(seconds, block, &lastTime);\
} while (0)
int main (int argc, const char * argv[])
{
BOOL done = NO;
// Do some long process
do {
@autoreleasepool {
// Do some work
done = work();
// Send progress updates
AllowOnceEvery(1.0, ^{
printf("Progress: %f", progress);
});
}
} while (!done);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment