Guides / Building Search UI / Going further

Conditional requests with InstantSearch iOS

By default, InstantSearch sends an initial request to Algolia’s servers with an empty query. This connection helps speed up later requests.

However, sometimes you don’t want to perform more network calls than are necessary. For example, you may want to limit the number of search requests and reduce your overall Algolia usage. This guide helps you build a UI that prevents this initial request.

Set Searcher shouldTriggerSearchForQuery property

All the Searcher implementations such as HitsSearcher and FacetSearcher provide the shouldTriggerSearchForQuery closure which defines the boolean condition for triggering a search operation. By default has a nil value so the search will be triggered on each search() function call.

The closure blocking searches for empty queries should look as follows:

1
2
3
4
5
6
let searcher = SingleIndexSearcher(appID: "YourApplicationID", 
                                   apiKey: "YourSearchOnlyAPIKey", 
                                   indexName: "search_index_name")
searcher.shouldTriggerSearchForQuery = {
  return $0.query.query != ""
}
Did you find this page helpful?