Implementing Diffable DataSource for Heterogeneous Items in iOS
Article Summary
Pattogató Bence from Wise Engineering tackles a problem every iOS dev faces: animating heterogeneous items in collection views without the usual headaches. His solution? A clever enum wrapper that makes UICollectionViewDiffableDataSource work with mixed content types.
The Wise engineering team needed to build their Insights feature with complex, multi-section collection views containing different item types (monthly summaries, legends, spending categories). Traditional approaches meant manual batch updates or CoreData dependencies. This article shows how they leveraged DiffableDataSource for cleaner, animated updates.
Key Takeaways
- Wrap heterogeneous items in value-associated enums to satisfy Hashable requirements
- Subclass UICollectionViewDiffableDataSource for type-safe cell configuration
- Use unique IDs to link view and data models for selective refreshes
- Separate view models from business logic using presenter pattern for cleaner code
By wrapping different item types in an enum, you can use DiffableDataSource for automatic animated updates without manual batch operations or CoreData dependencies.
About This Article
UICollectionViews don't have built-in diffing, so developers have to pick between writing manual performBatchUpdates calls, using Swift 5.1's ordered collection diffing, or adding CoreData just to animate insert, delete, reload, and move operations.
Bence Pattogatò wraps different item types like Day and Hour in value-associated enums and subclasses UICollectionViewDiffableDataSource to automatically find differences between snapshots and animate updates. No manual work needed.
The Wise Insights feature shows three item types (months, legend, spending categories) across multiple sections and can refresh specific parts without reloading everything. This removes the need for CoreData or batch update code while keeping the presenter pattern clean and organized.