Skip to content

Instantly share code, notes, and snippets.

@shishirthedev
Created January 23, 2019 08:33
Show Gist options
  • Save shishirthedev/30679846b892284e553830b043b9f7c5 to your computer and use it in GitHub Desktop.
Save shishirthedev/30679846b892284e553830b043b9f7c5 to your computer and use it in GitHub Desktop.
API SIDE:
1. Server needs to accept two paramters fromIndex and itemsLimit in the API url as query params.
2. Server response will provide an extra key totalItems to identify all items received or not.
And and array of items from fromIndex to itemsLimit.
APP SIDE:
1. First loadItem() will be called with fromIndex = 0 and batchSize = 20 (for example in viewDidLoad()
or viewWillAppear). removeAll items from privateList array before calling loadItem() for the first time
2. Server returns an array of first 20 items and totalItems total number of items in the server.
3. Append the 20 items in privateList array and reload tableView
4. In tableView:cellForRowAtIndexPath method check if the cell is the last cell. And check if totalItems (form server) is greater than privateList.count. That means there are more items in the server to load
5. if indexPath.row == privateList.count - 1 { // last cell
if totalItems > privateList.count { // more items to fetch
loadItem() // increment `fromIndex` by 20 before server call
}
}
6. Refresh after appending new items in the array when server response received.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment