From the course: Android Dependency Injection with Dagger 2 and Kotlin
Unlock the full course today
Join today to access over 22,600 courses taught by industry experts or purchase this course individually.
@Singletons and @Reuseable
From the course: Android Dependency Injection with Dagger 2 and Kotlin
@Singletons and @Reuseable
- [Instructor] Perhaps you've already noticed that every time we use Dagger to get an object, we get a new instance of the object type. In fact, when we ask a Dagger factory for a new object, it creates not only a new instance of the object we asked for, but new instances of every object in the dependency tree. That's a lot of objects. Dagger does provide a way to control object creation. It's called Scope. There are two built-in Scopes in Dagger, Singleton and Reusable. A Singleton error annotation guarantees that a factory that returns the annotated object will return only a single instance of that object, even in a concurrent environment. The code required to enforce that guarantee is a bit expensive, so Dagger also provides the Reusable Scope. A Reusable annotation causes Dagger to create a factory that is a bit less expensive, but that does not absolutely guarantee a single instance. In fact, in most cases it will return a single instance. It's just that it doesn't guarantee it…
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.