Skip to content

Multi-section diffing!

Latest
Compare
Choose a tag to compare
@jflinter jflinter released this 14 Apr 16:38
· 66 commits to master since this release

The Good News

One of the most common feature requests since Dwifft launched has been the ability to more easily manage UITableView/UICollectionViews with multiple sections. I am beyond pleased to announce that Dwifft 0.6 gains this feature! This is done by modeling your data with a new struct titled SectionedValues<Section, Value>. SectionedValues is akin to an ordered dictionary - it contains an array of tuples, where the first element in the tuple represents the section itself, and the second element is an array of elements that should be contained at that section. If you were mapping this to, say, the Contacts app, you might imagine each Section would be a letter of the alphabet, and the Values for each Section would be the contacts whose name started with that letter. SectionedValues can be a little annoying to construct, so they have some convenience initializers that can make that easier - you should read their documentation for more info.

TableViewDiffCalculator and CollectionViewDiffCalculator have been updated to reflect this. Instead of setting their rows property to an array, you now set their sectionedValues property to a, you guessed it, instance of SectionedValues. This will now make calls to insertSections and deleteSections as well as insert{Rows|Items} and delete{Rows|Items} on your view. If you'd like to see this all in action, look at the example app, which has been updated to use all the new APIs.

There's some other good stuff in this release too. Dwifft is now tested with SwiftCheck, which yields much stronger guarantees around the correctness of the underlying diff algorithm. Dwifft 0.6 is also much faster and memory-efficient (like, an order of magnitude). I've also really beefed up the documentation - it's now autogenerated with jazzy and I'm pleased to say has 100% documentation coverage.

All of this was really hard to accomplish, especially in a way that didn't have garbage performance. I don't know of another diffing library that supports multi-section changes! I want to thank everyone who gave their feedback on the design and implementation.

The Bad News

Dwifft 0.6 has breaking changes. Sorry not sorry, this is a pre-1.0 library (even though it's like 2 years old) and I'm still settling on its shape. Notably, I have removed or slightly changed several types and methods.

  • TableViewDiffCalculator and CollectionViewDiffCalculator now use the aforementioned sectionedValues property instead of rows. If you would like the old behavior back, for the purposes of migrating, use SingleSectionTableViewDiffCalculator/SingleSectionCollectionViewDiffCalculator, which behave ~identically to how TableViewDiffCalculator/CollectionViewDiffCalculator used to work.
  • Array.diff(otherArray) has been deprecated (extensions like that are bad). To do this you should now call Dwifft.diff(array1, array2) instead, which behaves exactly the same way.
  • The Diff struct has been removed - calls to diff now just return an array of DiffSteps (Diff was just a thin wrapper around this array, and is no longer necessary).
  • The LCS methods have been removed (if you don't know what I'm talking about, this doesn't affect you). These were mostly just useful for bootstrapping the original diff algorithm. If you were using them for something (I sort of doubt anyone was), drop me a line and we can spin them off into a separate library.

If you're having trouble migrating, drop me a line and I'll help.