Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix empty index path crash #77

Merged
merged 1 commit into from
Jun 1, 2020
Merged

Conversation

bryankeller
Copy link
Contributor

@bryankeller bryankeller commented May 16, 2020

Details

This fixes #76 - a bug where MagazineLayout was assuming that it would only receive "valid" index paths when requesting layout attributes. The crash would occur if a developer passed IndexPath() into layoutAttributesForItemAt. Flow layout avoids this crash by defaulting to IndexPath(item: 0, section: 0).

Part of me feels like this is just flow layout working around some weird behavior / maybe trying to mirror a workaround table view had from the beginning. The documentation for IndexPath looks like this:

extension IndexPath {

    /// Initialize for use with `UITableView` or `UICollectionView`.
    public init(row: Int, section: Int)

    /// Initialize for use with `UITableView` or `UICollectionView`.
    public init(item: Int, section: Int)

    /// The section of this index path, when used with `UITableView`.
    ///
    /// - precondition: The index path must have exactly two elements.
    public var section: Int

    /// The row of this index path, when used with `UITableView`.
    ///
    /// - precondition: The index path must have exactly two elements.
    public var row: Int

    /// The item of this index path, when used with `UICollectionView`.
    ///
    /// - precondition: The index path must have exactly two elements.
    public var item: Int
}

It specifically calls out that you should use the init(item: Int, section: Int) initializer when using UICollectionView. To me, this makes it seem like using IndexPath() is wrong / a programmer error.

====

Why does collection view / table view even use index paths? Seems like the wrong abstraction to me. IndexPaths allow indexing into arbitrarily deep tree structures, which seems completely overboard for a UI component that only has 2 layers (section and item).

Fun reminder than initializing IndexPaths is 35x slower than initializing a simple ElementLocation struct. See https://github.com/airbnb/MagazineLayout/blob/master/MagazineLayout/LayoutCore/Types/ElementLocation.swift#L23

Related Issue

#76

Motivation and Context

It matches UICollectionViewFlowLayout behavior, so I guess we should implement this workaround.

How Has This Been Tested

Types of changes

  • Docs change / refactoring / dependency upgrade
  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)

Checklist

  • My code follows the code style of this project.
  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • I have read the CONTRIBUTING document.
  • I have added tests to cover my changes.

@bryankeller bryankeller added the bug Something isn't working label May 16, 2020
@bryankeller bryankeller force-pushed the bk/fix-empty-index-path-crash branch from bd8a0f5 to 6b26297 Compare May 16, 2020 01:04
@bryankeller bryankeller merged commit 12dd2cc into master Jun 1, 2020
@bryankeller bryankeller deleted the bk/fix-empty-index-path-crash branch June 1, 2020 19:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

layoutAttributesForItem(at:) crashes when given an empty index path
2 participants