Matsushita Kohei
01/28/2020, 9:59 AMjw
01/28/2020, 11:57 AMaipok
01/28/2020, 12:53 PMsingle
is created upon request and returned same instance for every futher requests. But in Koin you can make single
instance inside particular scope, not only until the app is killed. It is much easier to keep instances unless you are in the particular scope of the app and close the scope once you don't need it. In worst case you can call close
on koin instance and it will release all the resources from existing koin
context.Artem Shumidub
01/28/2020, 3:23 PM@Scope
as a mechanism to handle scoping. Scoping allows you to “preserve” the object instance and provide it as a “local singleton” for the duration of the scoped component.
Annotation @Singleton
is just another scope. It was just there as the default scoping annotation. It provides your dependencies as a singleton as long as you are using the same component.Matsushita Kohei
01/28/2020, 3:45 PMobject SampleObject {
val instance = SampleClass()
}
To solve that problem↓
class SampleApplication: Application {
val instance = SampleClass()
….
}
But Koin doesn't keep instances in application classes.
Is the singleton classes of Koin destroyed by GC?aipok
01/29/2020, 1:12 PMApplication
class it would be more safe. But it is again the matter of a need. Application could stay in the memory also for a long time. With koin, you can call stopKoin()
at any time or reload the module/modules and it will clear all the instances of the classes or scoped instances could be used.Matsushita Kohei
01/29/2020, 1:33 PMaipok
01/29/2020, 1:41 PMobject
should be something that could not lock anything and that is not heavy. I'm using object classes only for easy access for grouped static functions. Not sure if there are some other use cases, but other than that I would prefer some scope for an object. And if there is no scope and object is not big, I could use single instance from DI(koin) over object
because I can replace it for tests for example. While object
you can't replace.Matsushita Kohei
01/29/2020, 1:47 PM