Swift - InstantSearch - Access to datasource

Hello everyone,

I already asked couple of question in this forum and since I am very happy with the support service, I would like to give a special thanks to @guy.daher and @cindy.cullen. I always get quick replies and useful information. Great jobs guys :slight_smile:

Coming to the actual business. I want to replace my algolia search with the InstantSearch. I already looked into the official documentation for this topic and downloaded examples as well but I am still struggling with an issue.

Earlier, I had this piece of code:

 algoliaSearch.fetchUsersBySearchValue(completionHandler: { (userArray) in
               self.searchUserSubView.setDataSource(userArray: userArray)
               self.searchUserSubView.frame.origin.y = self.textFieldContainer.frame.maxY
               self.view.addSubview(self.searchUserSubView)
           }, searchValue: searchTextField.text!, page: 0)

As you can see, I use the result of my search and pass the data source (setDataSource…) to a sub view. I need to do that. My sub view contains a table view that should display the results of the search.

Now, I want to do that exactly with the InstantSearch:

 required init?(coder: NSCoder) {
    challengeTableView = .init()
    
    userTableView = .init()
        
    let indices = [
        Section.users.index,
        Section.challenges.index,
    ]
    multiIndexSearcher = .init(client:.algoliaClient, indices: indices)
                  
    let hitsInteractors: [AnyHitsInteractor] = [
        HitsInteractor<Hit<User>>(infiniteScrolling: .on(withOffset: 10), showItemsOnEmptyQuery: true),
        HitsInteractor<Hit<Challenge>>(infiniteScrolling: .on(withOffset: 10), showItemsOnEmptyQuery: true),
    ]
                
    multiIndexHitsInteractor = .init(hitsInteractors: hitsInteractors)
    
    searchBarController = .init(searchBar: .init())
                  
    queryInputInteractor = .init()
       
    super.init(coder: coder)
    
    self.setupInstantSearchSettings()
}

 func setupInstantSearchSettings() {

    queryInputInteractor.connectSearcher(multiIndexSearcher)
    queryInputInteractor.connectController(searchBarController)

    multiIndexHitsInteractor.connectSearcher(multiIndexSearcher)
    multiIndexHitsInteractor.connectController(self)
       
    multiIndexSearcher.search()
}

When I type something now, requests are send toward Algolia but where can I see the data coming back, so I can pass the results to my sub view?

Hey @kaan548,

Thank you for the nice words!

So in order to get the data coming, you can checkout our docs about Multi Hits.

In there, you should see configureTableController which will show you how you can setup your tableView with the results being returned from Algolia. There are more customisation information that you can find in that page as well.

Also, if things are not working for you and nothing is being displayed, you can listen to multiIndexHitsInteractor.onError.subscribePast(with: self) { _, error in print(error)} to see what error you are getting.

1 Like