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

Keep paths loaded in memory for performance (e.g. in tab bar) #62

Open
jeanbaptistebeau opened this issue Nov 21, 2022 · 2 comments
Open

Comments

@jeanbaptistebeau
Copy link

jeanbaptistebeau commented Nov 21, 2022

Is there a way to keep some paths loaded in memory, i.e. not recreate the views when the path changes?

I was implementing a tab bar layout like in the RandomUsers example, but with this solution the pages get recreated every time the user switches tab, which can make the process of switching tab a bit laggy.

Is there a way to keep those pages in memory to provide a smooth tab bar experience?

Note: this would also allow keeping states, like scrolling positions. It just feels more natural.

@frzi
Copy link
Owner

frzi commented Nov 21, 2022

No, this is beyond the scope of SwiftUI Router and would probably require a significant rewrite of the framework. It also raises questions like what the state is based on. The path of the Navigator or the path of the Route (which may contain placeholders)?

Sadly as the framework currently works you're somewhat forced to keep track of states yourself (which I understand is very difficult when it comes to scrollviews et al) 😞

@stevenb9
Copy link

stevenb9 commented Dec 12, 2022

I was able to workaround this by using Factory dependency injection (https://github.com/hmlongco/Factory).

  1. Register your view as a singleton (static let myService = Factory(scope: .singleton) { View() as View }
  2. Inject the singleton view in your TabContents (@injected(Container.myService) private var map )
  3. For the route, hse this injected variable as the content (Route("/tabbar/map/*", content: map))

This will keep the view in memory

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

3 participants