Address Input Optimization: Autocomplete and Caching
Imagine a user typing “Tversk” and the system suggests 5 options in 300 ms. Without proper implementation, each keystroke triggers a separate API request—70% of users abandon the form if delays exceed 2 seconds. Average address entry time with autocomplete is 8 seconds, without it 35 seconds (per Nielsen Norman Group). The technical challenge is not just adding a library, but choosing the provider, configuring debounce, caching, and session tokens. Let's break down how to do it right.
How to Choose a Geocoding Provider?
| Provider | Strengths | Weaknesses |
|---|---|---|
| Google Places Autocomplete API | Best global coverage, POIs, businesses | Expensive at high traffic, weaker on building numbers in Russia |
| DaData | Best for Russian addresses (FIAS/KLADR), accuracy >95% | Only Russia |
| Nominatim (OpenStreetMap) | Free, global | No SLA, slower, 30% lower quality vs DaData |
| HERE Geocoding | Good in Europe, offline packages | More expensive than Google for small volumes |
| Yandex Geocoder | Good for CIS | Requires account, usage restrictions |
For most Russian projects, we recommend a DaData + Google combo: DaData as primary, Google as fallback for foreign addresses. This reduces request costs by 40% compared to using only Google. DaData is 2 times more accurate than Nominatim for Russian addresses—critical for logistics. Google Places with session token is 3–5 times cheaper than without.
Why Is the Session Token Important in Google Places?
On iOS, use the GooglePlaces pod and GMSPlacesClient.findAutocompletePredictions(fromQuery:filter:sessionToken:callback:). The key point is GMSAutocompleteSessionToken: one token per search session (from first character to result selection). This reduces costs 3–5 times compared to requests without a token. As the Google Places documentation states: "Using session tokens allows multiple requests to be grouped into a single billing."
let token = GMSAutocompleteSessionToken() let filter = GMSAutocompleteFilter() filter.type = .address filter.countries = ["RU", "BY", "KZ"] placesClient.findAutocompletePredictions( fromQuery: query, filter: filter, sessionToken: token ) { results, error in guard let results else { return } self.suggestions = results.map { $0.attributedFullText.string } } After selecting an address, call fetchPlace(fromPlaceID:placeFields:sessionToken:) to get coordinates—and reset the token. Without fetchPlace, coordinates are not available from autocomplete.
On Android, use Places.initialize(context, apiKey) + PlacesClient. In Jetpack Compose:
val placesClient = Places.createClient(context) val request = FindAutocompletePredictionsRequest.builder() .setQuery(query) .setSessionToken(AutocompleteSessionToken.newInstance()) .setTypesFilter(listOf(PlaceTypes.ADDRESS)) .setCountries("RU", "BY") .build() placesClient.findAutocompletePredictions(request) .addOnSuccessListener { response -> _suggestions.value = response.autocompletePredictions } What Does Debounce Give?
Without debounce, each keystroke triggers an API request. At an average typing speed of 4 characters per second, that's 4 requests instead of one. Our experience shows that proper debounce of 350 ms reduces the number of requests by 70%.
Step-by-step debounce implementation:
- Create a Publisher/Flow from the text field.
- Apply
debounce(for: 350 ms)operator. - Add a length filter (>= 3 characters).
- Use
flatMapLatestto cancel the previous request.
On iOS with Combine:
searchTextField.textPublisher .debounce(for: .milliseconds(350), scheduler: DispatchQueue.main) .removeDuplicates() .sink { [weak self] query in guard query.count >= 3 else { return } self?.fetchSuggestions(for: query) } On Android with StateFlow:
searchQuery .debounce(350) .filter { it.length >= 3 } .distinctUntilChanged() .flatMapLatest { fetchSuggestions(it) } .stateIn(viewModelScope, SharingStarted.Lazily, emptyList()) flatMapLatest cancels the previous request on new input—without it, old results may overwrite new ones.
Offline and Cache: How to Improve UX?
Store the last 10–20 selected addresses locally (UserDefaults / SharedPreferences) and show them when the input field is empty. This solves the most common case: users ordering delivery to the same address.
For search history, use Room / Core Data with columns address_string, lat, lon, last_used_at. On input, first search the local database (LIKE query), then concurrently request the API—show the local result immediately, replace with the API result when it arrives. Cache response time is 2–5 ms vs 200–500 ms from API.
Example full solution for iOS (SwiftUI + Combine)
class AddressSearchViewModel: ObservableObject { @Published var query = "" @Published var suggestions: [String] = [] private var cancellables = Set<AnyCancellable>() private let placesClient = GMSPlacesClient() private let token = GMSAutocompleteSessionToken() init() { $query .debounce(for: .milliseconds(350), scheduler: DispatchQueue.main) .removeDuplicates() .filter { $0.count >= 3 } .flatMapLatest { [weak self] query -> AnyPublisher<[String], Never> in guard let self = self else { return Just([]).eraseToAnyPublisher() } return Future { promise in let filter = GMSAutocompleteFilter() filter.type = .address filter.countries = ["RU"] self.placesClient.findAutocompletePredictions( fromQuery: query, filter: filter, sessionToken: self.token ) { results, error in guard let results = results, error == nil else { promise(.success([])) return } promise(.success(results.map { $0.attributedFullText.string })) } }.eraseToAnyPublisher() } .receive(on: DispatchQueue.main) .assign(to: &$suggestions) } func selectAddress(_ placeID: String) { let token = GMSAutocompleteSessionToken() let fields: GMSPlaceField = [.coordinate, .formattedAddress] placesClient.fetchPlace(fromPlaceID: placeID, placeFields: fields, sessionToken: token) { place, error in guard let coordinate = place?.coordinate else { return } // save coordinates } } } How to Test Autocomplete on Edge Cases?
Testing autocomplete catches errors that are hard to spot in production. We use a mock layer: replace API responses with test data (empty response, delays, errors). Check edge strings: empty string, single character, special characters (!@#$), very long addresses (200+ characters), addresses with non-standard letters (umlauts, Cyrillic). Also simulate network failures and timeouts—the app should gracefully show a fallback message and not crash. We have a checklist of 25+ scenarios for each project.
What's Included in the Work?
- Provider selection and integration (DaData, Google, Yandex).
- UI component development with dropdown list (SwiftUI / Jetpack Compose).
- Implementation of debounce, request cancellation, session tokens.
- Local caching of address history.
- Error handling: no network, API rate limits, invalid input.
- Testing on edge strings (empty, special characters, very long addresses).
- Integration documentation.
We have implemented address search in 30+ projects—from delivery services to geographic information systems. We guarantee stable operation under high load. Get your project evaluated—contact us for a consultation.
Timeline: two to four days—provider, UI, debounce, history cache, testing. Cost is calculated individually for your tasks. Get a consultation to find out how long your app's integration will take.







