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. You use a snapshot to set up the initial state of the data that a view displays, and you use snapshots to reflect changes to the data that the view displays.

The data in a snapshot is made up of the sections and items you want to display, in the order that you determine. 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, the code applies the snapshot, animating the UI updates between the previous state and the new state.

// Create a snapshot.
NSDiffableDataSourceSnapshot<NSNumber *, NSUUID *> *snapshot = [[NSDiffableDataSourceSnapshot alloc] init];


// Populate the snapshot.
[snapshot appendSectionsWithIdentifiers:@[@0]];
[snapshot appendItemsWithIdentifiers:@[[NSUUID UUID], [NSUUID UUID], [NSUUID UUID]]];


// Apply the snapshot.
[self.dataSource applySnapshot:snapshot animatingDifferences:YES];

For more information, see the diffable data source types:

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