I'm trying to display a list of songs using UICollectionView's List API. I would also like them grouped by the first letter of the song. At first, I thought I'd have something like this:
let sections = [
LibrarySection(name: A, songs: [
Song(name: "A Song"),
Song(name: "Another Song"),
]),
LibrarySection(name: B, songs: [
Song(name: "B Song"),
Song(name: "B another Song"),
]),
]
var snapshot = NSDiffableDataSourceSnapshot<LibrarySection, LibraryRow>()
snapshot.appendSections(sections)
sections.forEach { section in
snapshot.appendItems(section.songs, toSection: section)
}
dataSource.apply(snapshot, animatingDifferences: true)
But I facepalmed when I realized the smart thing to do is this:
let items = [
Song(name: "A Song"),
Song(name: "Another Song"),
Song(name: "B Song"),
Song(name: "B another Song")
]
The question I have is, what would the snapshot logic look like such that I am minimizing the amount of times I have to instantiate items and sections?
var snapshot = NSDiffableDataSourceSnapshot<LibrarySection, LibraryRow>()
???
dataSource.apply(snapshot, animatingDifferences: true)
Thanks for any feedback you may have!!!