Skip to content

Instantly share code, notes, and snippets.

@muizidn
Last active April 2, 2021 01:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save muizidn/e3ad553c7718bd88cad9bb44b11fb786 to your computer and use it in GitHub Desktop.
Save muizidn/e3ad553c7718bd88cad9bb44b11fb786 to your computer and use it in GitHub Desktop.
Paging Easy Swift
if indexPath.row == dataSource.count - 1 && dataSource.count < paging.total {
self.fetchPaging()
}
Api.fetch(page: paging.nextPage) { [weak self]res in
switch res {
case .success(let resp):
if !resp.data.isEmpty {
self?.paging.nextPage += 1
}
self?.paging.total = resp.total
case .failure(let err):
print("ERROR: \(err)")
}
}
var paging: (nextPage: Int, total: Int) = (1,0)

Paging Easy

  • Store nextPage and total
  • Only fetch in the last cell if dataSource.count < paging.total
  • Only assign nextPage if resp.data is not empty
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment