The new (iOS 13+) diffable datasources are great step forward making UITableView and UICollectionView much easier to use. Except one operation: item reloads.
Modern app architectures store data in some kind of ViewModel or ViewStore (The Composable Architecture, CombineFeedback, and many others), while the UI just observes the data.
Very likely, the ViewModel/ViewStore:
Thanks to the diffable datasources UITableViewDiffableDataSource and UICollectionViewDiffableDataSource, animated updating the table/collection view is very easy:
The only troubled step is the last one: Which items have to be reloaded?
DiffableWithReload automates identification of items that require reload, so you do not need to care about reloading. Cells needing reload (and only these) are automatically reloaded (when snapshot is applied).
DiffableWithReload subclasses UITableViewDiffableDataSource and UICollectionViewDiffableDataSource (still as generic classes) so you can use them with your data types.
For basic use, there are available:
TableViewDiffableReloadingDataSource<SectionIdentifierType: Hashable, ItemIdentifierType: Hashable, EquatableCellContent: Equatable>
CollectionViewDiffableReloadingDataSource<SectionIdentifierType: Hashable, ItemIdentifierType: Hashable, EquatableCellContent: Equatable>
For advanced use, when you need (for example) data locking, there are available these subclasses:
TableViewDiffableDelegatingDataSource<SectionIdentifierType: Hashable, ItemIdentifierType: Hashable, Delegate: ReloadingDataSourceDelegate, EquatableCellContent: Equatable>
CollectionViewDiffableDelegatingDataSource<SectionIdentifierType: Hashable, ItemIdentifierType: Hashable, Delegate: ReloadingDataSourceDelegate, EquatableCellContent: Equatable>
// change data in the cars array
cars.changeColorOfAllCars(brand: .volkswagen)
// apply the current snapshot
let snapshot = diffableDataSource.snapshot()
// items for reload are automatically created
diffableDataSource.applyWithItemsReloadIfNeeded(snapshot, animatingDifferences: true)
It is highly recommended to review the example code. There are 3 tabs in the example iOS application:
EquatableCellContent
(conforming to Equatable
)EquatableCellContent
) for each used cell in the table/collection view. Again, it can be any type conforming to Equatable
, but there are two handy structs creating equatable content:EncodableContent
creating Data?
value from the specified properties: Data?
is unique identifier of the displayed content and can be easily created from any Encodable
property of your underlaying data. The provided data
value is an Optional, because encode(to:)
may throw. In such a case the cell is always reloaded.HashableContent
creating Int
(the hash) value from the specified properties: hashValue
is not so unique identifier of the displayed content, however, may be good enough. Default choice should be EncodableContent
.To integrate Toast-Swift into your Xcode project using CocoaPods, specify it in your Podfile
:
pod 'DiffableWithReload', '~> 1.0.0'
and in your code use import DiffableWithReload
.
When using Xcode 11 or later, you can install DiffableWithReload
by going to your Project settings > Swift Packages
and add the repository by providing the GitHub URL. Alternatively, you can go to File
> Swift Packages
> Add Package Dependencies...
1.0.0
requires Swift 5link |
Stars: 4 |
Last commit: 2 years ago |
Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API | Analytics