Stop looking for NSOutlineViewDiffableDataSource, it’s here 👌
Here’s to the crazy ones…
At WWDC 2019 Apple introduced amazing APIs for UITableView
and UICollectionView
data management. For instance, the barely documented UITableViewDiffableDataSource
can be used as a data source of a table view, and all modifications made to its NSDiffableDataSourceSnapshot
are automatically applied by that table view.
These diffable APIs were fantastic news for UIKit Developers. Unfortunately AppKit has got only NSCollectionViewDiffableDataSource
and NSTableViewDiffableDataSource
which means that NSOutlineView
is out of luck. Let’s hope that Apple releases NSOutlineViewDiffableDataSource
at WWDC21.
This Swift Package is my modest attempt to implement a diffable data source for NSOutlineView
with support for nested items. This is how it’s used:
let dataSource = OutlineViewDiffableDataSource (outlineView: outlineView)
var snapshot = dataSource.snapshot()
snapshot.appendItems([folder1, folder2])
snapshot.appendItems([file11, file12], into: folder1)
snapshot.appendItems([file21, file22], into: folder2)
dataSource.applySnapshot(initialSnapshot, animatingDifferences: false)
The accompanying DiffableDataSourceSnapshot
API is heavily inspired by Cocoa’s own NSDiffableDataSourceSnapshot
and includes many similar methods, but adopted for tree data structures:
// …
mutating func insertItems(_ newItems: [Item], beforeItem: Item) -> Bool
mutating func insertItems(_ newItems: [Item], afterItem: Item) -> Bool
mutating func deleteItems(_ existingItems: [Item]) -> Bool
mutating func deleteAllItems()
mutating func reloadItems(_ items: [Item]) -> Bool
// …
Not sure how this ends, but the DiffableDataSourceSnapshot
is a value type. Just like its “official” counterpart, this struct can be built and applied from the background thread, as far as I know 😅
The Demo Mac app is a playground for breaking the library. The snapshot API is 100% covered by Tests, but I would much appreciate any help with QA.
If you download and play with the sample app, it’s already cool. But if you report or even fix one of the issues, you are just awesome. Here is a Roadmap, please join me if you can:
OutlineViewItem
master
branchNSOutlineViewDiffableDataSource
, why notYou can always find me as @VadimShpakovski in Twitter. If you publish apps in the App Store or in the Mac App Store, check out my native client for the App Store Connect: NativeConnect. Some people like it. Thanks in advance!