Skip to content
This repository has been archived by the owner on Aug 24, 2019. It is now read-only.

Thoughts on adding a simple macro based solution (for non-persisted)? #6

Closed
mhuusko5 opened this issue Jun 11, 2015 · 2 comments
Closed

Comments

@mhuusko5
Copy link

Macro

#define RateLimit(SECS) \
({ \
    static NSDate *lastCall = nil; \
    ^(dispatch_block_t block) { \
        @synchronized(lastCall) { \
            if (!lastCall || fabs(lastCall.timeIntervalSinceNow) > SECS) { \
                lastCall = NSDate.date; \
                block(); \
            } \
        } \
    }; \
})

Usage

RateLimit(60.0)(^{
    // Do some work that runs a maximum of once per minute
});
@soffes
Copy link
Owner

soffes commented Jun 11, 2015

Thanks @mhuusko5. Not a big fan though. In my opinion, macros are a code smell. This also won't work with Swift. Feel free to use in your own project though!

@soffes soffes closed this as completed Jun 11, 2015
@mhuusko5
Copy link
Author

Fair enough :)

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants