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 supplementary reuse bug by ensuring we track the right view instance in our displayed views cache #321

Merged
merged 4 commits into from
Aug 31, 2021

Conversation

kyleve
Copy link
Collaborator

@kyleve kyleve commented Aug 31, 2021

This PR fixes a bug introduced in #288 (well sort of, 288 uncovered the actual bug):

A long time ago, a method argument was renamed from view to anyView, which meant we were no longer shadowing the view variable on the Delegate:

        func collectionView(
            _ collectionView: UICollectionView,
                                         // Was renamed from `view`.
            willDisplaySupplementaryView anyView: UICollectionReusableView,
            forElementKind kindString: String,
            at indexPath: IndexPath
            )
        {
            let container = anyView as! SupplementaryContainerView
            let kind = SupplementaryKind(rawValue: kindString)!
            
            let headerFooter : PresentationState.HeaderFooterViewStatePair = ...

            headerFooter.willDisplay(view: container)

          - self.displayedSupplementaryItems[ObjectIdentifier(view)] = headerFooter
          + self.displayedSupplementaryItems[ObjectIdentifier(container)] = headerFooter

So the fix here was to properly pass the ObjectIdentifier of the container, not view (self.view is actually the ListView).

Without this change, didEndDisplay() was never invoked on the header/footer (because displayedSupplementaryItems is used to track what is being used by the collection view), meaning we never cleared out values during view reuse, which broke the next time a view was reused if the view type was different.

@kyleve kyleve changed the title Fix supplementary reuse bug by ensuring we track the right view instance in our displayed views cache [WIP] Fix supplementary reuse bug by ensuring we track the right view instance in our displayed views cache Aug 31, 2021
@@ -64,6 +64,8 @@ final class SupplementaryContainerView : UICollectionReusableView
) as! SupplementaryContainerView

view.reuseCache = reuseCache

// TODO: We need to also set this during on-screen updates; I don't think it's updated correctly now.
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

To be fixed after; but found this when debugging. It's theoretical; but I think it can happen.

@@ -163,7 +163,7 @@ extension ListView

headerFooter.willDisplay(view: container)

self.displayedSupplementaryItems[ObjectIdentifier(view)] = headerFooter
self.displayedSupplementaryItems[ObjectIdentifier(container)] = headerFooter
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This is the fix! Grr.

@@ -89,7 +89,9 @@ class SupplementaryContainerViewTests: XCTestCase

// Add a header

view.headerFooter = self.newHeaderFooter()
let header = self.newHeaderFooter()
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Since this is now weak; need a strong ref to the header.

@kyleve kyleve requested review from bizzemfrog and kylebshr August 31, 2021 22:37
Copy link
Collaborator

@kylebshr kylebshr left a comment

Choose a reason for hiding this comment

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

LGTM - will let you decide if the non-necessary changes should go in this PR

@kyleve kyleve changed the title [WIP] Fix supplementary reuse bug by ensuring we track the right view instance in our displayed views cache Fix supplementary reuse bug by ensuring we track the right view instance in our displayed views cache Aug 31, 2021
@kyleve kyleve merged commit 0e55435 into converge/0.25.1 Aug 31, 2021
@kyleve kyleve deleted the kve/fix-supplementary-reuse-bug branch August 31, 2021 23:07
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.

2 participants