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

Get notified when a ScreenID is no longer valid? #78

Open
ZevEisenberg opened this issue Nov 22, 2021 · 3 comments
Open

Get notified when a ScreenID is no longer valid? #78

ZevEisenberg opened this issue Nov 22, 2021 · 3 comments

Comments

@ZevEisenberg
Copy link
Contributor

Question

Is it possible to find out when the screen for a particular ScreenID is permanently dismissed? By "permanently" I mean that if the same screen is presented again, it would have a different ScreenID?

Problem description

I'm wrapping an analytics library that has strong ties to UIKit. In particular, it has the concept of a "page" object that is tied to the lifecycle of a UIViewController in memory. I'm trying to replicate this behavior with SwiftUI Views. I can't use onDisappear because that is triggered if a view is covered by a nav push, and in UIKit, views don't get deallocated when they're covered by navigation.

I tried attaching a @StateObject to my views, and having it notify on deinit, but @StateObject seems not to make guarantees about when or if it will deallocate things.

My current thinking is to keep a dictionary that maps ScreenIDs to my analytics page objects, and remove them from the array when a ScreenID becomes invalid. But I would need the composable navigator to tell me when that happens, and I'm not sure if that's possible. I'm thinking something like:

// naming subject to discussion
Navigator.Datasource(root: someRoot, screenIDDidBecomeInvalid: { screenID in
})

I'm not super familiar with the composable navigator's API surface, so maybe there's a better place to put it, but that's the gist of what I'm looking for.

@ohitsdaniel
Copy link
Contributor

Did you try an onDismiss PathBuilder?

https://github.com/Bahn-X/swift-composable-navigator/blob/main/Sources/ComposableNavigator/PathBuilder/PathBuilders/PathBuilder+OnDismiss.swift

I think, that's what you're looking for. It it isn't, let me know. :)

@ZevEisenberg
Copy link
Contributor Author

This looks promising. Is there somewhere I can stick an onDismiss that will notify me of any dismissal anywhere in the tree? I didn't see an obvious way to do it, and I want to avoid sprinkling onDismiss on all my path builders if possible.

@ohitsdaniel
Copy link
Contributor

You could subscribe to the datasource path publisher and diff on it. There is no guarantee that only a single screen is dismissed at a time (going back to a previous screen, multiple steps before the current one) and the on dismiss view also subscribes above mentioned changes.

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

No branches or pull requests

2 participants