https://kotlinlang.org logo
#dagger
Title
# dagger
w

william

04/09/2019, 11:36 AM
any recommended ways about eagerly creating a dagger provided object? i probably don't want dagger to create it, but in my application i have an
@Inject
field that is never used just so my object gets created - this seems odd though
g

gergo

04/09/2019, 11:57 AM
may wrap it in Lazy like @Inject Lazy<Foo> foo; https://google.github.io/dagger/api/2.10/dagger/Lazy.html
p

Paul Woitaschek

04/09/2019, 12:28 PM
Just delete your field if it's not used
w

william

04/10/2019, 1:37 AM
@gergo what would i gain from having it lazy? i want it to start when my application starts (android) since its sort of a background task.
@Paul Woitaschek the field isn't used but the injection is utilized to create the object (and thus start the background service / task thing)
p

Paul Woitaschek

04/10/2019, 5:18 AM
So you want an object not to be created but created? I don't get it
Anyways, dagger creates things. You could use a providing method on your component
g

gergo

04/10/2019, 7:04 AM
ohh i missundestood your intentions then. well if you don't want to inject it, then you may provide it from the component, and create it like component.provideFoo() in your application's onCreate method which will create the object but you won't have the field. you want something like this right?
w

william

04/10/2019, 5:09 PM
@Paul Woitaschek i want it to be created but there is no use for me to have a reference to that object. i.e. the object sets up a listener in its init method
p

Paul Woitaschek

04/11/2019, 3:41 PM
Then don't do it in the init block
Have a init() method and use a provisioning method on your component and call init() on it, that's more explicit than having a class that magically starts doing things by instantiating it
12 Views