I'm building UICollectionView
with new diffable NSDiffableDataSourceSnapshot
and NSDiffableDataSourceSectionSnapshot
. I need to use NS…SectionSnapshot
to populate my UICollectionView
with outline items.
First, I create NS…Snapshot
, set all section and root items and apply this snapshot, then for each section that contains outline items I create NS…SectionSnapshot
, set all items here and apply these snapshots.
Pseudocode:
var snapshot = NSDiffableDataSourceSnapshot<Section, Item>()
snapshot.appendSection(sections)
snapshot.appendItems(items1, toSection: section1)
snapshot.appendItems(items2, toSection: section2)
dataSource.apply(snapshot, animatingDifferences: false)
…
var sectionSnapshot1 = NSDiffableDataSourceSectionSnapshot<Item>()
sectionSnapshot1.append([rootItem1])
sectionSnapshot1.append(outlineItems1, to: rootItem1)
…
dataSource.apply(sectionSnapshot1, to: section1, animatingDifferences: false)
…
This first step works correct.
But then I receive the new data from the server, parse and store it to my view model and trigger new update — I receive the crash.
Pseudocode for update:
// This part completely identical to the very first setup of dataSource (see above)
// Note, that at this point some cells may be deleted or inserted by dataSource because I change the snapshot.
// (For example, I have 3 values and I construct the specific row for them; if there are no values I show one-line text "No data".)
// Here I just reconfigure some existing items
var snapshot = dataSource.snapshot()
snapshot.reconfigureItems(items)
dataSource.applySnapshot(snapshot, animatingDifferences: false)
First error I received is on line (when I try to apply "root items" snapshot):
dataSource.apply(snapshot, animatingDifferences: false)
Error message:
*** Assertion failure in -[_UITreeDataSourceSnapshotter childrenForParentAtIndex:recursive:], _UITreeDataSourceSnapshotter.mm:256
Thread 1: "Invalid parameter not satisfying: globalIndex < self.count"
Well, I think it's trying to say that "You're applying snapshot without outline items, so there is some problem". But why? I disable the animation and the next line is all about applying these outline items…
The next thing I did is to remove this line (applying "root items" snapshot) and try to only apply the sections I need. But, when I apply the first section it also triggers the update of some other cells in other section. The problem is that at this point I don't have my previous data and I receive the crash. Again, why?
As I understand my main problem is how to apply all snapshots at once to prevent all this errors? OR there is other right way to manage this kind of layout that I should use?