In a UIKit listings collection view using a diffable data source, every live price update makes rows fade out and back in; why, and how should the updates be applied?
answer
- identity versus content
- a new hash is a new item
- replace the cell or keep it
- iOS 15 on the snapshot
basics
~20 sRows are replaced, not updated: the whole listing, price included, is the item identifier, so a new price is a new item, or the code calls reloadItems(:). Key items by stable ID and call reconfigureItems(:) (iOS 15) to update cells in place.
solid answer
~50 sA diffable data source diffs by identity. If the identifier is a `Hashable` struct that includes `price`, the changed listing is a different value, so `apply` sees the old item gone and a new one inserted and animates a delete and an insert, with a fresh cell. Fix identity first: make the identifier the listing's ID and keep mutable fields in a store the cell registration reads. Then mark the content change. `reloadItems(_:)` replaces the cell with a newly dequeued one; `reconfigureItems(_:)`, added in iOS 15, keeps the existing cell (prefetched ones too) and runs the provider on it without calling `prepareForReuse()`, resizing self-sizing cells afterwards. Apple's documentation says to prefer reconfiguring unless you need a different cell type. Applying with `animatingDifferences: false` still diffs but does not animate the resize. With an index-path data source the matching calls are `reconfigureItems(at:)` and `reconfigureRows(at:)`.
code
swift · 14 linesimport UIKit
// The whole model is the item identifier, so its price is part of its identity.
struct ListingRow: Hashable { let id: UUID; var price: String; var isFavourite: Bool }
enum Section: Hashable { case listings }
@MainActor
func showRows(_ rows: [ListingRow], in dataSource: UICollectionViewDiffableDataSource<Section, ListingRow>) {
var snapshot = NSDiffableDataSourceSnapshot<Section, ListingRow>()
snapshot.appendSections([.listings])
snapshot.appendItems(rows, toSection: .listings)
// A row whose price changed hashes differently: the diff sees one item deleted and another inserted.
dataSource.apply(snapshot, animatingDifferences: true)
}go deeper
Recall that a diffable data source compares identifiers, and that reconfiguring updates a cell while reloading replaces it.
Explain why a Hashable model identifier turns content changes into deletes and inserts, and how reconfigureItems(:) differs from reloadItems(:).
Diagnose the fade from symptoms, move identity to stable IDs with a store, and know the documented limits of reconfiguring: same cell type, no prepareForReuse(), resizing.
Make IDs-not-models and reconfigure-for-content team rules, and decide how frequent live updates are batched on list screens.
## Identity versus content A **diffable data source** compares the identifiers in the new snapshot with the identifiers on screen. It knows nothing about your model's fields: two identifiers are "the same item" exactly when they are equal and hash the same. That gives two separate questions for every update: - **Identity**: is this still the same listing? That is decided by the identifier. - **Content**: has something shown in its cell changed? A snapshot only learns this when you say so. The fade comes from getting the first question wrong or answering the second one with the wrong tool. ## Diagnosing the fade Work through the likely causes in order: 1. **Look at the item identifier type.** If it is `NSDiffableDataSourceSnapshot<Section, ListingRow>` where `ListingRow` is a `Hashable` struct holding `price` and `isFavourite`, every price tick produces a value that is not equal to the old one. The diff therefore reports a deletion and an insertion, and the collection view animates both, with a newly dequeued cell. 2. **Look for `reloadItems(_:)`.** Even with ID identifiers, reloading tells the data source to replace the item's cell with a new one, which also redraws it from scratch. 3. **Look for full resets.** Calling `applySnapshotUsingReloadData(_:)` on every tick skips the diff and resets the whole view, interrupting any running animations. ## Reconfigure versus reload | | `reconfigureItems(_:)` | `reloadItems(_:)` | |---|---|---| | Available | iOS 15 | iOS 13 | | The cell | the existing cell is kept, including prefetched ones | replaced by a newly dequeued cell | | `prepareForReuse()` | not called, per the documentation | the new cell comes through the normal dequeue path | | Cell type | the provider must return the same type and the same existing cell | may return a different cell type | | Self-sizing | cells are resized after reconfiguring | the new cell sizes itself | | Apple's advice | the default for content changes | only when you need an entirely new cell | With a custom index-path data source the equivalents are `reconfigureItems(at:)` on `UICollectionView` and `reconfigureRows(at:)` on `UITableView`; with a diffable data source, the documentation says to use the snapshot method. ## The update flow 1. Store listings by ID, for example `[Listing.ID: Listing]`, and make the snapshot's item type `Listing.ID`. 2. When prices arrive, update the store first. 3. Take the current state with `dataSource.snapshot()`. 4. Keep only the IDs that the snapshot still contains, since a listing may have been removed meanwhile, and call `reconfigureItems(_:)` with them. 5. `apply(_:animatingDifferences:)`. The registration handler runs on each existing cell and reads the new price from the store. Listings that have no cell at the moment need nothing: their next configure reads the store. ## Rules and limits of reconfiguring - The provider must dequeue the **same cell type** for the index path; a listing that turns into a "sold" cell of another class needs `reloadItems(_:)`. - Because `prepareForReuse()` is skipped, the configure path must set every field itself, which is the rule for cells anyway. - `animatingDifferences` decides whether size or layout changes caused by reconfiguring animate; `UIView.performWithoutAnimation(_:)` inside the configuration avoids animating specific properties. - A favourite toggle is the same flow: update the store, reconfigure that one ID, apply. ## Checking the update in a test A snapshot reports what you marked on it. `reconfiguredItemIdentifiers` and `reloadedItemIdentifiers` (both iOS 15) list the identifiers flagged for each kind of update, and `itemIdentifiers` gives the order. A unit test can feed a price tick to the code that builds the snapshot and assert that exactly the changed IDs were reconfigured and none were reloaded, so a regression back to `reloadItems(_:)` or to model identifiers fails in the test rather than on a device. The diffable data source's `description()` is listed in its documentation under debugging, which helps when the live screen and the test disagree. ## Why this is UIKit-specific The general idea of diffing by stable ID is covered in the system design material on list rendering. What this question tests is UIKit's version of it: the `Hashable` identity of snapshot identifiers, the iOS 15 split between reconfiguring and reloading, and the documented skip of `prepareForReuse()` when a cell is reconfigured.
- How do you stop a reconfigure from animating a height change?Pass `animatingDifferences: false` when applying the snapshot; the documentation says that flag controls whether size and layout changes from reconfiguring animate. To skip animation only for certain properties, set them inside `UIView.performWithoutAnimation(_:)` in the cell configuration.
- When is reloadItems(_:) still the right call?When the item needs a different cell, for example a listing that switches to a sold-out cell class, or you deliberately want a fresh cell. Reconfiguring requires the provider to return the same existing cell, so a type change must go through reload.
- Prices change every second for hundreds of listings; what else do you do?Coalesce each tick into one snapshot and one apply instead of one apply per listing, reconfigure only IDs the snapshot contains, and keep the store as the truth so listings without a cell pick up the price when next configured. Apply on the main actor.
- reloadItems(_:) and reconfigureItems(_:) do the same thing under two names.
- Changing a field inside a Hashable identifier struct updates that row in place.
- reconfigureItems(_:) calls prepareForReuse() on each cell before configuring it.
- Calling applySnapshotUsingReloadData(_:) on every tick is the clean fix for the fade.
- Reconfiguring works only for cells currently on screen, not prefetched ones.