declare scoped instances in Koin
What’s a scope?
A scope is a fixed duration of time in which an object exists. When the scope context ends, any objects bound under that scope cannot be injected again (they are dropped from the container).
Declare a scoped definition
By default in Koin, we have 3 kind of scopes:
singledefinition, create an object that persistent with the entire container lifetime (can’t be dropped).
factorydefinition, create a new object each time. No persistence in the container (can’t be shared).
scopeddefinition, create an object that persistent tied to the associated scope lifetime.
To declare a scope definition, use the
Declare and use a scope
A scope is a logical unit of scoped definitions.
For the given classes:
We can write the following scope:
Create and use a scope instance
Koin instance or also available from your
getKoin(), the following functions allows to handle scope instances:
Below are some further readings: