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:
Go with Google Places. They have a dedicated Places Autocompletion API. There is even a complete library for iOS on GitHub.
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.
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;
How to do a local search autocomplete as Apple's native map app does?
MkLocalSearchRequest will not perform auto complete on your search string, perhaps because Apple wants to limit the number of requests from 3rd party apps.
In theory you could reverse-engineer the requests and responses to https://gsp-ssl.ls.apple.com/auto_complete.arpc and then perform those requests yourself, not using MkLocalSearchRequest at all. But that would probably lead to your app being rejected on the App Store.
How to get estimated time of arrival to multipe destinations on iOS?
Hi investigate class MKRoute
this class contains all information you need.
This object contains
expectedTravelTime
Also you should consider LoadingThrottled
The data was not loaded because data throttling is in effect. This
error can occur if an app makes frequent requests for data over a
short period of time.
For prevent your request from bing throttled, reduce number of requests.
Try to use Completion Handlers to know if you request is finished and only after send another request or cancel previous. From my experience try to handle this request just as regular network request just be sure you are not spamming unnecessary requested to the Apple API. But this is not 100% guarantee that Apple won't throttle your requests.
Related Topics
Disable Warning Dialog If Bluetooth Is Powered Off iOS
How to Use Nsurlsession to Determine If Resource Has Changed
How to Detect Whether an iOS Device Is in Silent Mode or Not
Game Engine Collison Bitmask... Why 0X01 etc
Swift- Custom Uitableviewcell Delegate to Uiviewcontroller Only One Protocol Works
How to Hide a Bar Button Item for Certain Users
Googleutilities/Appdelegateswizzler/Private/Gulapplication.H' File > Not Found
Swift, Dispatch_Group_Wait Not Waiting
Swift Spritekit Facebook Share Button
Insert String at Cursor Position of Uitextfield
Making Phone Calls on the Iphone
How to Detect the Scroll Direction from the Uicollectionview
How to Properly Send an Image to Cloudkit as Ckasset
How to Detect When User Used Password Autofill on a Uitextfield
How We Can Set the Light Content Style of Status Bar in iOS 9 for Whole Application
How to Differentiate Whether a User Is Tapping the Screen with His Finger or an Apple Pencil