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

When I must close scope? #425

Open
JorikFat opened this issue Feb 15, 2021 · 1 comment
Open

When I must close scope? #425

JorikFat opened this issue Feb 15, 2021 · 1 comment

Comments

@JorikFat
Copy link

JorikFat commented Feb 15, 2021

I dont use ViewModel, and want to handle lifecycle by TP.
I have Activity and complex model with inner state(i can't save it in Bundle)

  • If I open scope by Instance of Activity - it will be closed on onDestroy()[by smoothie].
  • If I open scope by constString - i need to close scope manualy.

Which way is correct?

@ts14ic
Copy link

ts14ic commented Jun 26, 2021

This is probably highly dependent on your own use-case.
Toothpick doesn't treat either way as correct per se.
You pick the option that will work for you, depending on how you organized your dependencies.

When you call openScope(something), you're essentially creating a global variable.
This global variable can only be accessed with the same thing that you passed into openScope initially.
It's like a key.

Toothpick doesn't care if you pass an activity or a string to the openScope method.
If you call openScope with an activity, then you can access dependencies inside that global container only with the very same activity (exact same this object).
When you call closeScope, that global variable is released.

Smoothie lifecycle utility has the same effect as calling closeScope inside your onDestroy method manually.
You can do the same in a BaseActivity class.
You can do it just like Smoothie does with Lifecycle Observer - it's a feature of the Android, not Toothpick.
Nothing special (I don't even use Smoothie).

Now, where you openScope and closeScope, that depends on how you structure your classes and their dependencies.

Remember that Activity-ies must not be stored in global variables (normally).
When Android calls onDestroy, it wants to free memory occupied by the Activity.
If you store that Activity inside some global variable, you impede Android from doing what it wants.
Eventually, all memory will be consumed, and the app will crash with an out of memory exception.

That's why closeScope is needed.
It's a function to destroy the global variable that's holding onto the Activity, for example.

p.s.
You mentioned Bundle.
Again, Toothpick is just holding a sort of global variable - it only exists as long as the app is running.
If you restart the app - variables will be recreated from scratch.
If your classes depend on an Activity, and that activity stores something, then your data is lost when the Activity is re-created.
That can happen if you rotate the screen, change locales, the app goes to sleep etc - many reasons.

Toothpick exists to enforce a proper DI pattern, with proper constructors, to better organize code, make dependencies clear, make the code easier to test. Toothpick is not a data storage solution :)

If you want data to surive, find a way to store data in a Bundle - it will survive screen rotations then, for example.
Find a way to store data in SharedPreferences - it will survive app restarts then.
Use a database for data that is not suitable for SharedPreferences.
When you recreate your dependencies, restore data from where you saved them.

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