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

Make a lazy registerSingletonWithDependencies #338

Closed
feinstein opened this issue Aug 17, 2023 · 5 comments
Closed

Make a lazy registerSingletonWithDependencies #338

feinstein opened this issue Aug 17, 2023 · 5 comments

Comments

@feinstein
Copy link

If we have a Singleton that depends on another Singleton that has an async initialization, then we can't use lazy singletons, which leads to memory leaks.

@escamoteur
Copy link
Collaborator

Can you explain to me how a lazy singleton would help you in this aspect?
I'm pretty sure there are better ways to achieve the same?
Did you have a closer look at get_it scopes?

@feinstein
Copy link
Author

feinstein commented Aug 17, 2023

If we declare Singletons they will be create as soon as they are declared and will live in memory. Scopes allow us to reduce their footprint, but then we have to be controlling scopes all over the app. I don't want to use scopes for several reasons:

1 - I have a Storage class that's used in many screens and flows in my app, but not in all of them. The ones who use it, I want them to have the same Singleton with the same internal cache. It's acceptable if it's created once by one screen and it lives there in memory, the problem is when all the singletons are created at startup and bloat the memory. If I use scopes, I will have to be micro-managing the scopes across the entire app, and I would also like to share singletons across scopes.

Let's say I have pages A, B, C, D and E. I want the same Storage on pages A, B and C, but not on D and E. Those pages are in a bottom navigator, so they are not related to each other.

2 - The less coupling my app has on third party libraries, the better. No offense, get_it is very useful, but I rather "contain" how much it influences my app in case I need to replace it one day. I declare the dependencies when the app starts and I make all the singletons and factories call each other. then I create the objects I want on a given page. This way I only have get_it in 2 places: App Initialization and Pages initialization.

@escamoteur
Copy link
Collaborator

escamoteur commented Aug 17, 2023 via email

@feinstein
Copy link
Author

feinstein commented Aug 17, 2023

unless they cache images or other huge Data blocks.

So you agree with me that are cases where Singletons can bloat your app's memory, so why not make the lib ready to provide to us the tools to control the memory in any way we want?

@escamoteur
Copy link
Collaborator

See our other discussion

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