Learn and Master ⚔️ Diffable Datasources in 5 Minutes

Whoever has worked with UITable- or UICollection-Views in the past will have seen error messages like the following probably more often:

Invalid update: invalid number of items in section 0. The number of items contained in an existing section after the update (22) must be equal to the number of items contained in that section before the update (34), plus or minus the number of items inserted or deleted from that section (23 inserted, 5 deleted) and plus or minus the number of items moved into or out of that section (3 moved in, 5 moved out).

UICollection- and UITableViews make it not necessarily easy to exchange, add, update or remove new cells based on data models. Due to this fact, some frameworks have been developed in the last few years to eliminate the complexity involved in this process and to establish an approach of data-driven lists in iOS. These include IGListKit, DeepDiff, DifferenceKit and many other projects on Github. Probably this could be a reason why Apple came up with the idea to develop something of their own in 2019.

At the WWDC 2019 time had come. The DiffableDatasources have seen the light of day. If you don’t feel like watching the linked video, take the 5 minutes for the article and you’re ready for easier UICollection- and UITable-Views in your own apps.

Even though SwiftUI already fixes a lot of these old problems, there are probably still many apps out there that will follow the classic approach for a few years. These are the people the blog post is addressed to.

Diffable Datasources

We start with a simple UICollectionView setup. Each of you should understand this so far. The UICollectionView is linked to a UICollectionView in the storyboard which contains a simple UICollectionViewCell with the ReuseIdentifier “Cell”

ViewController

To work with DiffableDatasources, we first need data models that reflect the sections and the actual entries in those sections. The elements must be hashed so that the Datasource can distinguish the elements from each other and find out which objects are already in the list.

Models

Next, we will build a CellProvider. This is just a simple block that receives a CollectionView, IndexPath, and the corresponding DataModel. It configures a cell and then returns it. We define it as a stored property within the ViewController. Supplementary-Views are added in the same way as cells via a block.

CellProvider

We can define the DiffableDatasource now. It is a generic class of type UICollectionViewDiffableDataSource which need to use the previously defined types for Section and Item.

Now we only need a function to get the data into the UICollectionView.
In the first step we create a snapshot of the current data source using the snapshot method. Then we have to define the Sections and the Items we want to add to them. Finally we only need to call apply.

Adding Items

Full Code

Full Code Example

Conclusion

In my opinion, Diffable-Datasources are a completely under-represented feature in the iOS Environment. They minimize the errors that occur when handling lists under iOS enormously and they also make updates to Collection- and Table-Views much much easier. Another big benefit is that they also eliminate the use of third-party libraries and provide a more sustainable native approach.

It’s a wrap 🎁

Congratulation, you learned the basics of Diffable Datasources.
Happy Coding 🎉

If the article has helped you and you want me to continue writing similar articles, you are welcome to support me with a small donation

🤜🏾 🤛

Feel free to add me on github, twitter, linkedin or xing if you have any questions. If you like electronic music you can also listen to my Tracks on SoundCloud ;)

--

--

--

Stories and technical tips about building apps for iOS, Apple Watch, and iPad/iPhone

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Sebastian Boldt

Sebastian Boldt

Creative Head, iOS Developer @ Immowelt, DJ/Producer from Hamburg. Creator of HabitBe, a weekly Habit Tracker written in SwiftUI. https://www.sebastianboldt.com

More from Medium

Guide to manage secrets in SwiftUI app

New configuration file

Guide How To Display Modal Alert on iOS With Help of UIAlertController

Alert vs ActionSheet by Apple

Rain+Lightning Animation using SpriteKit in SwiftUI (Part 1)🌧️

What is Swift Language and advantages and disadvantages?