https://kotlinlang.org logo
#android
Title
# android
m

Matsushita Kohei

01/28/2020, 9:59 AM
Hi everyone 👋 How does the Android DI library hold singleton objects? For example, Dagger, Koin, etc. are stored internally just by passing Context. I want to know the internal processing. We also to be aware garbage collection 😢
stackoverflow 5
👀 1
j

jw

01/28/2020, 11:57 AM
Dagger knows nothing about Context. Singleton in Dagger just means initialize once and return the same instance. You still need to keep the component instance around for as long as you want the singletons to live.
👍 1
a

aipok

01/28/2020, 12:53 PM
In Koin
single
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.
a

Artem Shumidub

01/28/2020, 3:23 PM
Dagger 2 has annotation
@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.
m

Matsushita Kohei

01/28/2020, 3:45 PM
If the following is defined, will the instance be destroyed?(Because of GC)
Copy code
object SampleObject {
  val instance = SampleClass()
}
To solve that problem↓
Copy code
class SampleApplication: Application {
  val instance = SampleClass()
  ….
}
But Koin doesn't keep instances in application classes. Is the singleton classes of Koin destroyed by GC?
a

aipok

01/29/2020, 1:12 PM
Object is the same static object at the end of all and as any other static objects it will be there. Of cause if put an instance into
Application
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.
👍 1
m

Matsushita Kohei

01/29/2020, 1:33 PM
thanks for your reply. Your advice was helpful. If a singleton is used on the library side in an application composed of multiple modules, is it better to have a singleton instance in "object"?
a

aipok

01/29/2020, 1:41 PM
object
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.
👍 1
m

Matsushita Kohei

01/29/2020, 1:47 PM
Thank you for your kindness!