airbnb / MagazineLayout

A collection view layout capable of laying out views in vertically scrolling grids and lists.
Apache License 2.0
3.3k stars 219 forks source link

Fix data source index OOB crash #110

Closed bryankeller closed 2 years ago

bryankeller commented 2 years ago

Details

This is an alternative fix / approach to fixing the data source count / internal model state mismatch that occurs when UICollectionView requests layout attributes before the layout has been notified about incoming batch updates.

This bug became an issue after the layout loop fix PR was merged last week. That PR changed how we create layout attributes (no longer done in prepareLayout, and instead done lazily as layout attributes are requested for specific index paths). This behavior change assumed that UICollectionView would never try to request layout attributes for out-of-bounds index paths. Unfortunately, this is not a safe assumption.

This PR changes our layout attributes creation code so that it returns nil if we detect an out-of-bounds index path. This is effectively how MagazineLayout behaved prior to the layout loop fix PR. Reverting this behavior has no impact on the layout loop fix, which will continue to work.

Also note that I'm not bumping the version / pushing a new release. I'll verify this works properly in the Airbnb iOS app, and if all looks good, then I'll push a new tag / make a new release.

Related Issue

N/A

Motivation and Context

Fix a crash.

How Has This Been Tested

Example app

Types of changes

Checklist