Infinite scroll or lazy fetching from Algolia server

0

I have two uses cases. First, I would like to display the results of a fetch from the algolia server in a table view. Whenever I scroll down to the end, another fetch should be send to get new data.

Second, I have another table view that contains different sections/categories. Whenever I click on a specific section, I want to just load the respective “part” of the objects from algolia. Currently maximimum amount of objects should be 50. But here again, I don’t want to load 50 objects at once. They should be loaded according to the scrolling of my table view.

I have following code to do the “lazy fetch”

func collectionView(_ collectionView: UICollectionView, willDisplay cell: UICollectionViewCell, forItemAt indexPath: IndexPath) {

if (challengeArray.count > (10 * i)) {
    PAGINATION = PAGINATION + 1
    i = i + 1
}
else {
    return
}

// if last row of the table view is reached, load the next items
if indexPath.row  == challengeArray.count - 1 {

    algoliaSearch.fetchChallengeInformationBySearchValue(completionHandler: { (challengeArray) in

        // if there is no data, we can return from the function
        if (challengeArray.count == 0) {
            return
        }

        for challengeObject in challengeArray {
            let results = self.challengeArray.filter { $0.id == challengeObject.id}
            if (results.count == 0) {
                self.challengeArray.append(challengeObject)
            }
        }
        self.mainCollectionView?.reloadData()

    }, searchValue: "", page: PAGINATION) 
}

}
I am trying to use a value for PAGINATION that increases whenever a specific number (10) of objects are loaded and I scroll down to the end to fetch the next 10 objects.

Can anyone help me with my both use cases, please?
I know there is an example for infinite scroll. However, I need to do that within my IOS application. The examples were about javascript.

Hello,

Thank you for contacting us!

I would suggest you use InstantSearch instead of the API Client, as you will get infinite scrolling for free. Here is our docs around InstantSearch and our getting started guide which shows how to do infinite scrolling.

Hello guy,

thanks for the reply. I added ‘InstantSearch’ into my podfile. After installing and opening my Xcode, I get following issue:

The “Swift Language Version” (SWIFT_VERSION) build setting must be set to a supported value for targets which use Swift. This setting can be set in the build settings editor.

That depedency error pops up in context with the InstantSearch. Could you tell me, how to solve this issue?

Thanks!

Hey @kaan548, I ll answer in InstantSearch for Swift 4.1 - Compile problem after installing pod