1

Given two arrays of RandomModelObject that conform to Codable, Equatable and Hashable I want to calculate a diff between them and animate content changes in a UICollectionView. Having to support iOS 11 made me pick https://github.com/tonyarnold/Differ as a dependency for doing so.

This code:

class ScreenNameCollectionViewDataSource {
    var elements: [RandomModelObject] = []
}

extension ScreenNameViewController: ScreenNameViewModelDelegate {
    func elementsStoreUpdated() {
        collectionView.animateItemAndSectionChanges(oldData: dataSource.elements,
                                                    newData: viewModel.blablabla, 
                                                    updateData: {
            dataSource.elements = viewModel.blabla
        })
    }
}

Produces 2 errors:

Instance method 'animateItemAndSectionChanges(oldData:newData:indexPathTransform:sectionTransform:updateData:completion:)' requires that 'RandomModelObject.Element' conform to 'Equatable'

Instance method 'animateItemAndSectionChanges(oldData:newData:indexPathTransform:sectionTransform:updateData:completion:)' requires that 'RandomModelObject' conform to 'Collection'

The errors don't seem to point me anywhere - Array is a Collection and the model conforms to Equatable. Did I miss anything there?

mgapinski
  • 483
  • 2
  • 7

1 Answers1

1

You are using animateItemAndSectionChanges, which not only requires that T is a Collection, but it also requires that T's elements are Collections. In other words, T needs to be something like a 2D array.

This is because animateItemAndSectionChanges handles both rows and sections. The 2D collection will tell the method what the old and new rows and sections are. Each "inner" collection represents a section.

Since your data source is a 1D array, it seems like you just need animateRowChanges, which is for single-section table views.

If each RandomModelObject actually represents a section, then you need to map each one of those to an array, so that you get a [[Something]], and change the updateData closure accordingly.

Sweeper
  • 213,210
  • 22
  • 193
  • 313
  • oh, that was something that I did not figure out by reading the docs, animateItemChanges worked just fine. Thanks! – mgapinski Aug 17 '20 at 12:13