MKDirections many requests stop working MKDirections many requests stop working xcode xcode

MKDirections many requests stop working


I experienced the same doing a unit test.

What I usually do with such APIs is using a rate limiter. It limits the number of calls to an API in a given timeframe.

There are several implementations, I successfully use and like this one https://github.com/nuclearace/SwiftRateLimiter

It lets you choose the number of calls and the timeframe. (You can even use two instances for one API so say: at most 10 calls per second and at most 100 calls per minute)

I don't like the direction.isCalculating approach too much, because it makes synchronizing your threads hard for the general case.A simple approach is to wait for an answer before making the next call.after you got the answer, directions.isCalculating obviously is false.

But I prefer the rate limiter.

Apple is throttling "poorly written/misbehaving Apps", but I'm not aware that Apple documented the rate limit it uses to do so.

code example:

let GlobalDirectionsRateLimiter = RateLimiter(tokensPerInterval: 3, interval: 1.0)GlobalDirectionsRateLimiter.removeTokens(1) {_,_ in    directions.calculate { (response: MKDirections.Response?, error: Swift.Error?) in    ...    }}

Don't trust my parameters for the rate limiter. All calculations in my app come from user actions, so I don't need more.