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 issue where supplementary elements don't track section changes #404

Merged
merged 4 commits into from
Jul 1, 2017

Conversation

Adlai-Holler
Copy link
Member

@Adlai-Holler Adlai-Holler commented Jul 1, 2017

Unfortunately, since supplementary elements are keyed by section index, we have to migrate supplementaries when sections are inserted/deleted. So if you have section 0 with a header, and you insert a section at index 0, that previous header should now be at index path e.g. {1, 0}.

So we migrate all supplementary elements when there are section-level changes. This is meant to be a minimal diff to fix the crash, and it was reduced down from the larger change #405 which unifies our index change tracking.

Resolves #397 and (I believe) #385 @Pacek @mrvincenzo

Copy link
Member

@appleguy appleguy left a comment

Choose a reason for hiding this comment

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

Thanks for the extremely fast fix! Also thanks to @Pacek for creating the sample project in #397.

[supps removeObjectForKey:oldIndexPath];
} else {
// Section index changed, move it.
NSIndexPath *newIndexPath = [NSIndexPath indexPathForItem:oldIndexPath.item inSection:newSection];
Copy link
Member

Choose a reason for hiding this comment

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

I think in this case we may also need to do the removeObjectForKey:oldIndexPath ?

Copy link
Member Author

Choose a reason for hiding this comment

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

Ah good catch!

@mrvincenzo
Copy link

@Adlai-Holler I've verified that a5a9627 fixes #385. Thanks a lot!

@appleguy
Copy link
Member

appleguy commented Jul 1, 2017 via email

@Adlai-Holler
Copy link
Member Author

Adlai-Holler commented Jul 1, 2017

Note: Unfortunately there's still a subtle bug here!

Imagine you have sections 0 1, you delete section 0, and during the dictionary enumeration you visit in the order 1 0. Visit 1: remove:1 update:0, then visit 0: remove:0 – now we've lost the header for section 0 that we just established, and we have no headers left.

I'll update the diff to be a little less eager about performance and simply generate a new dictionary every time without erasing elements.

@Adlai-Holler
Copy link
Member Author

I've updated the diff and tested it again against the sample project from @Pacek in #397 to make sure it still works.

@Pacek
Copy link

Pacek commented Jul 1, 2017

@appleguy and the rest of the Texture gang, you guys are the best, thanks for fixing this so fast.

Copy link
Contributor

@maicki maicki left a comment

Choose a reason for hiding this comment

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

❤️

// For each index path of that kind, move entries into a new dictionary.
// Note: it's tempting to update the dictionary in-place but because of the likely collision between old and new index paths,
// subtle bugs are possible. Note that this process is rare (only on section-level updates),
// that this work is done off-main, and that the typical supplementary element use case is just 1-per-section (header).
Copy link
Member

Choose a reason for hiding this comment

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

@Adlai-Holler this is a very worthwhile comment, thanks!

bernieperez pushed a commit to AtomTickets/Texture that referenced this pull request Apr 25, 2018
…extureGroup#404)

* Fix collection view supplementary issue

* Add a fast-path

* Remove the old index path entry unconditionally

* Handle overwriting bug
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Crashes when removing sections from ASCollectionNode
5 participants