Fix "Child already has a owner" Assert #242
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Upgrading to the latest version of Yoga introduced an assert in
YGNodeInsertChild()
that was previously removed accidentally (see relevant commit). This assert will throw an error if the node we are trying to add already has an owner. Unfortunately, we can fall into this case quite easily if we move a view between different superviews (as demonstrated in the following contrived example).To avoid triggering this assert we should check inFlex.addItem(_ view: UIView)
whetherview
already has a superview, and if so remove the view's associated yoga node as a child from the superview's associated yoga node. This approach mirror's how UIKit handles adding subviews, and should help ensure that Yoga and UIKit's representation of the view hierarchy remains in syncUPDATE - Restoring the changes from this old commit (c303faa) should ensure that any lingering parent references are cleaned up before we call
YGNodeInsertChild()