Why Do I Get an Mkerrordomain Error When Doing a Local Search

Why do I get an MKErrorDomain error when doing a local search?

What you're seeing here is the MKError.loadingThrottled error. You will have to delay requests you're sending to Apple.

You can do this by restarting a timer every time the user updates the search query. You can adjust how frequently you ping the API by extending the length of the timer. By resetting the timer each time the query is updated, you avoid sending multiple requests when the characters are changing rapidly.

// declare and store Timer somewhere

func searchAddress(with query: String) {

}

func timerDidFire(_ sender: Any) {
let query = textField.text
searchAddress(with: query)
}

Timer.scheduledTimerWithTimeInterval(0.5, target: self, selector: #selector(timerDidFire), userInfo: nil, repeats: false)

iOS app getting throttled from local searches

Autocompletion requires a special APIs. MapKit doesn't offer such an interface. Just firing off dozens of requests to the normal search API causes a tremendous load.

You basically have two options:

  1. Go with Google Places. They have a dedicated Places Autocompletion API. There is even a complete library for iOS on GitHub.

  2. Reduce the number of requests, e.g. by only sending a request if the user has paused typing for 300ms and only if no earlier request is outstanding. But that's still no guarantee that Apple won't throttle your requests.

kCLErrorDomain error 2 after geocoding repeatedly with CLGeocoder

I believe the reason is the following:

Apple's geocoder does not answer every request in the same way. Instead, the first requests from a certain device are answered quickly, but if the device has sent say 100 requests or more, the answers arrive slower and slower or requests are not answered at all, which might cause your error.

When you reload the view controller, this simply takes time, and the geocoding server is more willing to answer again.
Essentially, you cannot do anything about it, since the geocoder sever wants to protect itself from being overloaded by requests from a single device. You simply had to limit the number of requests that you send there.

BTW: The docs say "you should not send more than one geocoding request per minute".

How to throttle search (based on typing speed) in iOS UISearchBar?

Thanks to this link, I found a very quick and clean approach. Compared to Nirmit's answer it lacks the "loading indicator", however it wins in terms of number of lines of code and does not require additional controls. I first added the dispatch_cancelable_block.h file to my project (from this repo), then defined the following class variable: __block dispatch_cancelable_block_t searchBlock;.

My search code now looks like this:

- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
if (searchBlock != nil) {
//We cancel the currently scheduled block
cancel_block(searchBlock);
}
searchBlock = dispatch_after_delay(searchBlockDelay, ^{
//We "enqueue" this block with a certain delay. It will be canceled if the user types faster than the delay, otherwise it will be executed after the specified delay
[self loadPlacesAutocompleteForInput:searchText];
});
}

Notes:

  • The loadPlacesAutocompleteForInput is part of the LPGoogleFunctions library
  • searchBlockDelay is defined as follows outside of the @implementation:

    static CGFloat searchBlockDelay = 0.2;



Related Topics



Leave a reply



Submit