Slide 90
Slide 90 text
Scope - Constructor?
If a class part of DriverSessionScope implements Scoped and injects DriverSessionService (which is
app-scoped), then we end up in a livelock and the app freezes. There is nothing wrong with this setup and it should be
supported.
The chain of events is as follows: the app scope gets created and someone injects DriverSessionService. Dagger
attempts to create DriverSessionService. In the constructor the service immediately creates the
DriverSessionScope, when the driver is already logged in. As part of this routine the service injects all Scoped
instances contributed to the DriverSessionScope. If one of these instances injects DriverSessionService (again,
from the app scope and that's valid), then Dagger cannot provide the DriverSessionService, because the previous
instance hasn't been created yet and is still in the constructor call. Therefore, Dagger creates another
DriverSessionService and the whole loop repeats.
TL;DR: Don't run any logic in the constructor.
The solution is implementing Scoped and moving all logic from the constructor call into onEnterScope().