1

I have UITableViewController that supports both pagination and pull to refresh.

I am trying to understand the correct way to handle

  1. Replacing all the data - pull to refresh
  2. Append new items - pagination

I have an update method like the below.

When the data is updated from a pull to refresh or loaded for the first time, the refresh flag is true and on a pagination update it is false.

func update(with list: UserList, animate: Bool = true, refresh: Bool) {

  var snapshot: NSDiffableDataSourceSnapshot<Section, UserList>

  if refresh {
    snapshot = NSDiffableDataSourceSnapshot<Section, UserList>()
    snapshot.appendSections(Section.allCases)
  } else {
    snapshot = dataSource.snapshot()
  }

  snapshot.appendItems(list.active, toSection: .active)
  dataSource.apply(snapshot, animatingDifferences: animate)
}

I am unsure if this is correct.

I am creating a new empty snapshot on refresh, appending sections, adding items and applying.

On pagination I am grabbing the existing dataSource snapshot and adding my items to the end.

Is this the correct use of dataSource.snapshot() or is it in fact OK to simply append to an empty snapshot each time?

Teddy K
  • 820
  • 1
  • 6
  • 17

0 Answers0