Fix NavigationView
broken state after re-render
#259
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.
After #241 was merged,
NavigationView
started using@ObservedObject
instead of@State
for its destination storage. This uncovered a bug where@ObservedObject
properties that have their values provided in-place lost their subscriptions after a re-renderer. This manifested in navigation links not working in the demo after any scene phase changes that trigger a re-render. This was caused by a newNavigationContext
being created on every render, but a correspondingMountedCompositeElement
instance kept dangling subscription to the oldNavigationContext
instance, while new subscriptions weren't created.This PR splits subscriptions on
MountedCompositeElement
into separate "transient" and "persistent" arrays. Persistent subscriptions are set up once when an element is mounted, and are automatically cleaned up when the element is unmounted. Scene phase and color scheme subscriptions onMountedApp
are now primary examples of these persistent subscriptions, but@StateObject
is another future use case to be implemented in a separate PR. Transient subscriptions are recreated on every re-render to allow new@ObservedObject
instances to establish their subscriptions.NavigationView
in the demo still resets to the empty destination view when the scene phase observer is triggered, butNavigationLink
selections are fixed now. Resetting to the empty destination view on scene phase changes will be fixed when@StateObject
implementation is available in a separate PR.