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 Incorrect Item/Cell FoundDuring WillDisplay and DidEndDisplaying Callbacks #146

Conversation

brynbodayle
Copy link
Contributor

@brynbodayle brynbodayle commented Aug 14, 2023

Change summary

  • fixes an issue discovered in the Airbnb app where while during a collection view update, the collection view will provide a didEndDisplay item callback for a cell that is only in the new data, this would cause a runtime assertion and incorrect view callbacks from being propagated

How was it tested?

How did you verify that this change accomplished what you expected? Add more detail as needed.

  • Wrote automated tests
  • Built and ran on the iOS simulator
  • Built and ran on a device

Pull request checklist

All items in this checklist must be completed before a pull request will be reviewed.

  • Risky changes have been put behind a feature flag, e.g. CollectionViewConfiguration
  • Added a CHANGELOG.md entry in the "Unreleased" section for any library changes

@brynbodayle brynbodayle force-pushed the bb-item-display-callbacks-during-updates-fix branch from d3d65a0 to 763bf10 Compare August 14, 2023 22:16
@brynbodayle
Copy link
Contributor Author

Will be following-up with the same fix for supplementary items, wanted to split these PRs up.

///
/// This is used in cases where the collection view might be mid-update and we need to find the underlying item for a cell and index
/// path, but there is no guarantee of whether the cell is from the pre-update data or post-update data (so we check both).
private func data(for cell: CollectionViewCell, at indexPath: IndexPath) -> CollectionViewData? {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

but there is no guarantee of whether the cell is from the pre-update data or post-update data (so we check both).

collection view bein' collection view

guard
let item = data?.itemIfPresent(at: indexPath),
let section = data?.sectionIfPresent(at: indexPath.section)
let item = data?.item(at: indexPath),
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Notice how the willDisplay callback didn't call the ifPresent variant of this method. I believe this is because someone ran into this issue I just fixed in the past, but decided to fix it by calling the non-asserting function (the ifNeeded variant).

With this more robust fix, we can bring this in parity with willDisplay and add a helpful assert back.

let item = data?.itemIfPresent(at: indexPath),
let section = data?.sectionIfPresent(at: indexPath.section)
let item = data?.item(at: indexPath),
let section = data?.section(at: indexPath.section)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we delete func itemIfPresent(at indexPath: IndexPath) -> AnyItemModel? in CollectionViewData now?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah yes, good catch!

/// The item path of the cell from its last configuration update. Used to associate the view with the underlying data. When collection
/// view provides view display callbacks, if it is mid update, we need this to see if the view came from pre-update data or
/// post-update data.
var itemPath: ItemPath?
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should probably make the same fix in CollectionViewReusableView for supplementary views

Copy link
Contributor Author

@brynbodayle brynbodayle Aug 14, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fast follow, see above PR comment ;)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

love a fast follow - SGTM!

Copy link
Contributor

@bryankeller bryankeller left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Glad this works around the issue! I've seen this kind of "I have no way of knowing if I should reference old or new data" issue before at the layout level. A little bird told me Apple has private API for resolving some of these ambiguities. classic.

Left a few comments, but otherwise LGTM!

@brynbodayle brynbodayle force-pushed the bb-item-display-callbacks-during-updates-fix branch from 763bf10 to 47aca71 Compare August 14, 2023 22:29
Copy link
Contributor

@thedrick thedrick left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thank you @brynbodayle and @bryankeller !

@brynbodayle brynbodayle force-pushed the bb-item-display-callbacks-during-updates-fix branch 2 times, most recently from 3259834 to 972494e Compare August 15, 2023 00:38
@brynbodayle brynbodayle force-pushed the bb-item-display-callbacks-during-updates-fix branch from 972494e to ae2df9d Compare August 15, 2023 01:47
@brynbodayle brynbodayle merged commit 4be15df into airbnb:master Aug 15, 2023
9 checks passed
@brynbodayle brynbodayle deleted the bb-item-display-callbacks-during-updates-fix branch August 16, 2023 16:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants