Signature
About this widget
Use theHits component to display a list of search results.
The component automatically reloads results when new hits are fetched from Algolia.
HitsConnector is a generic class you can customize by implementing a class or structure that represents your record and conforms to the Codable protocol.
If, for some reason, the engine can’t parse a record into the type you provided,
the onError event of the Interactor instance will be triggered.
If you prefer to deal with raw JSON objects, set JSON as the type of record and use the rawHitAtIndex(_ row: Int) -> [String: Any]? method to access a hit.
See also:
Examples
Define the custom type representing the hits in your index conforming toCodable protocol.
Swift
HitsConnector and launch an initial search on its searcher.
Swift
Parameters
The logic applied to the hits.
The
FilterState for your filters.The ID of your application.
Your application’s search-only API key.
Name of the index to search.
Whether infinite scrolling is enabled.
offset defines the threshold for loading the next “page” of results.
For example, if the index of the last visible hit is 10,
and you setoffset to 3, the engine will display the next page if the hit at index 13 (10+3) isn’t loaded.
If set to .off, this parameter turns off infinite scrolling.If
false, no results are displayed when users haven’t entered any query text.The controller interfacing with a concrete hits view.
Low-level API
To fully control theHits components and connect them manually, use the following components:
Searcher. Handles your searches.HitsInteractor. The logic applied to the hits.HitsController. The controller that interfaces with a concrete hits view.FilterState. The current state of the filters.
Swift
- The
Searcherreceives new results and transmits them toHitsInteractor - The
HitsInteractorparses search results and notifiesHitsController - The
HitsControllerrefreshes the view presenting the hits
HitsController
The default controllers,HitsTableViewController and HitsCollectionViewController,
let you create a basic Hits view based on UITableView and UICollectionView components from UIKit.
You must configure these controllers using the TableViewCellConfigurable/CollectionViewCellConfigurable implementation protocols that define how your hit model is bound to a concrete cell class.
Here is an example of the implementation using the UITableViewCell and CustomHitModel.
Swift
CellConfigurable implementation.
Swift
Customization
You can subclass theHitsTableViewController or HitsCollectionViewControllerto customize their behavior.
Swift
Customizing your view
The default controllers,HitsTableViewController and HitsCollectionViewController,
work well when using native UIKit components with default behaviors.
If you want to use another component as a hits view or introduce some custom behavior to the already provided UIKit component,
you can create a controller conforming to the HitsController protocol.
Protocol
var hitsSource: DataSource?:
Reference to an entity providing a list of hits.
func reload():
Function called when a reload of the hits view is required.
func scrollToTop():
Function called when scrolling to the top of the hits view.
Implementation example
Swift
SwiftUI
InstantSearch provides theHitsList SwiftUI view, which you can embed in your views.
It uses HitsObservableController as a data model.
HitsObservableController is an implementation of the HitsController protocol adapted for usage with SwiftUI.
HitsObservableController must be connected to the HitsConnector or HitsInteractor like any other HitsController implementation.
You should define the view representing a single hit.
Swift
HitsObservableController as a data model.
It provides the hits property to streamline the design process of your custom SwiftUI view.
The notifyAppearanceOfHit(atIndex) function of HitsObservableController might be called on the appearance of each hit to ensure the correct operation of the infinite scrolling feature.
Result metadata and Hit structure
Each hit returned by Algolia is enriched with search metadata, likehighlightResult, objectID, and snippetResult.
InstantSearch provides an easy way to parse these using the Hit wrapper structure.
This generic structure encapsulates your record type and gives strongly typed access to a hit’s metadata.
For example, consider the following record structure:
Swift
Codable protocol, the record is ready to use with the HitsInteractor as follows:
Swift
Hit structure.
Swift
Movie object by accessing the object field of Hit:
Swift
Hit structure gives access to the following fields:
Swift