Skip to content
This repository has been archived by the owner. It is now read-only.
Simple utility for only executing code every so often.
Swift Ruby Objective-C
Branch: master
Clone or download

Latest commit

Latest commit dfda80c Jun 19, 2018


Type Name Latest commit message Commit time
Failed to load latest commit information.
Example Clean up Nov 10, 2016
RateLimit.xcodeproj update to Swift 4 Dec 18, 2017
RateLimit Version 2.1.2 Jun 18, 2018
Tests Much simpler debouncing Nov 10, 2016
.gitignore Update gitignore Oct 20, 2016
.swift-version update to Swift 4 Dec 18, 2017
.travis.yml travis, update xcode version Dec 19, 2017
Gemfile Remove coverate report for now Sep 11, 2015
Gemfile.lock Update gems Sep 17, 2016
LICENSE Update year Sep 17, 2016
Rakefile Test on macOS so Travis doesn't timeout Oct 20, 2016
RateLimit.podspec Version 2.1.2 Jun 18, 2018
Readme.markdown update readme swift version Dec 19, 2017


Rate Limit

Version Build Status Swift Version Carthage compatible CocoaPods compatible

Simple utility for only executing code every so often.

This will only execute the block passed for a given name if the last time it was called is greater than limit or it has never been called.

This is really handy for refreshing stuff in viewDidAppear: but preventing it from happening a ton if it was just refreshed.

Rate Limit is fully thread-safe. Released under the MIT license.


We’ll start out with a TimedLimiter:

// Initialize with a limit of 5, so you can only use this once every 5 seconds.
let refreshTimeline = TimedLimiter(limit: 5)

// Call the work you want to limit by passing a block to the execute method.
refreshTimeline.execute {
    // Do some work that runs a maximum of once per 5 seconds.

Limiters aren’t persisted across application launches.

Synchronous Limiters

TimedLimiter conforms to the SyncLimiter protocol. This means that the block you pass to execute will be called synchronously on the queue you called it from if it should fire. TimedLimiter uses time to limit.

CountedLimiter is also included. This works by taking a limit as a UInt for the maximum number of times to run the block.

The SyncLimiter protocol has a really neat extension that let’s you do things like this:

let funFactLimiter = CountedLimiter(limit: 2)
let funFact = funFactLimiter.execute { () -> String in
    // Do real things to get a fun fact from a list
    return "Hi"

Now funFact is a String?. It’s just an optional of whatever you return from the block. The returned value will be nil if the block didn’t run.

You can of course make your own SyncLimiters too!

Asynchronous Limiter

One AsyncLimiter is included. You can make your own too. The included async limiter is DebouncedLimiter. This is perfect for making network requests as a user types or other tasks that respond to very frequent events.

The interface is slightly different:

let searchLimiter = DebouncedLimiter(limit: 1, block: performSearch)

func textDidChange() {

You would have to setup the limiter in an initializer since it references an instance method, but you get the idea. The block will be called at most once per second in this configuration.

Pretty easy!

Open up the included Xcode project for an example app and tests.



Carthage is the recommended way to install Rate Limit. Add the following to your Cartfile:

github "soffes/RateLimit"


Add the following to your Podfile:

pod "RateLimit"

Then run pod install.

You can’t perform that action at this time.