Creating Data Snapshots | raywenderlich.com


This is a companion discussion topic for the original entry at https://www.raywenderlich.com/5429927-beginning-collection-views/lessons/6

@pasanpr, please give a bit more detailed explanation of the meaning of ‘number’ argument in the UICollectionViewDiffableDataSource trailing closure.

@andreysvx When you created the diffable data source the type parameters were specified as Section and Int.

UICollectionViewDiffableDataSource<Section, Int>!

In this case Int is the type of each value that our data source will display. The trailing closure defined when creating an instance of that data source takes three arguments - the collection view that the data is going to be displayed in, the index path of the cell where the data is going to be displayed, and the value of the data that is going to be displayed in the cell at that particular index path.

We know that this value is going to be of type Int since that’s what was specified in the definition. Since you can give closure arguments any local constant names we give it the name number since we know the values being displayed are just numbers.

It doesn’t have to be number it can be any name. Does that make sense?

1 Like

Thank you! It is much more clear. What still remains unclear for me is how UICollectionViewDiffableDataSource “knows” this data. I mean the data to be displayed by collection view is defined later by initiating an array. It seems to be a reversed order of doing things… or not?:thinking:

UICollectionViewDiffableDataSource only knows that it is a value of type Int because it’s specified in the type parameters. What we’re defining is a closure so at that point, you’re right, the data source does not know how many values are there and what index paths it needs to call it for. But it does save the closure for execution later.

When the data source does have data it iterates through each value in the data set and passes it as an argument to the stored closure.

What you are defining is a function that says when you eventually have data this is how I expect you to set it up

1 Like

thank you! Very clarifying! :smiley:

The download materials link only downloads the beginning project. The final project files are the same as the beginning project files

@pasanpr Can you please help with this when you get a chance? Thank you - much appreciated! :]

This seems like a big jump from previous courses. It requires you to understand generics and hashables but I don’t think this is explained in any earlier courses in this learning path. Have I missed some episodes from earlier courses that explain these things?

@brigden17 Do you still have issues with this?