Concepts / Building Search UI / Conditional display
Oct. 11, 2019

Conditional Display

Handling no results

When a search returns no results, you might want to display specific content bringing the user back to a successful search. To do this, you should subscribe to the Searcher’s onResponse and show your no-results UI when the response has no hits.

Example

1
2
3
4
5
6
7
searcher.onResults.subscribe(with: self) { (_, results) in
    if results.hits.isEmpty {
        showNoResultsUI()
    } else {
        hideNoResultsUI()
    }
}

Handling errors

When an error occurs, you might want to display some specific content helping the user go back to a search that was successful.

To be notified of errors, subscribe to the Searcher’s onError field.

1
2
3
4
searcher.onError.subscribe(with: self) { (_, args) in
    let (_, error) = args
    debugPrint("Error occured\(error)")
}

Handling empty query

By default InstantSearch iOS will always show you results even when the query is empty. In some case you may want to hide the results on the empty query.

To help you with that, the Hits widget exposes the showItemsOnEmptyQuery property in the HitsInteractor’s initializer. This property defaults to true. When it is set to false, the widget will not present hits when the query entered by the user is empty.

1
let hitsInteractor = HitsInteractor<JSON>(showItemsOnEmptyQuery: false)

Did you find this page helpful?