Contents

NSDiffableDataSourceSnapshotReference

A representation of the state of the data in a view at a specific point in time.

Declaration

class NSDiffableDataSourceSnapshotReference

Overview

Diffable data sources use snapshots to provide data for collection views and table views. Through a snapshot, you set up the initial state of the data that displays in a view, and later update that data.

The data in a snapshot is made up of the sections and items you want to display, in the specific order you want to display them. You configure what to display by adding, deleting, or moving the sections and items.

To display data in a view using a snapshot:

  1. Create a snapshot and populate it with the state of the data you want to display.

  2. Apply the snapshot to reflect the changes in the UI.

You can create and configure a snapshot in one of these ways:

  • Create an empty snapshot, then append sections and items to it.

  • Get the current snapshot by calling the diffable data source’s snapshot() method, then modify that snapshot to reflect the new state of the data that you want to display.

For example, the following code creates an empty snapshot, and populates it with a single section with three items. Then, it applies the snapshot, animating the UI updates between the previous state and the new state represented in the snapshot.

For more information, see the diffable data source types:

Bridging

Avoid using this type in Swift code. Only use this type to bridge from Objective-C code to Swift code by typecasting from a snapshot reference to a snapshot:

let snapshot = snapshotReference as NSDiffableDataSourceSnapshot<Int, UUID>

Topics

Creating a snapshot

Getting item and section metrics

Identifying items and sections

Inserting items and sections

Removing items and sections

Reordering items and sections

Reloading data