has anyone had success setting up the workmanager ...
# koin
m
has anyone had success setting up the workmanager capabilities in Koin? the docs are kinda confusing me and I don't really understand what they're saying
c
I'm using koin with WM and it works. I'm not at my laptop right now but I'll post a snippet later. I think there's a sample in the Koin repo that I took inspiration from.
a
I've always just done
single { WorkManager.getInstance(androidContext()) }
. You can
inject
the instance and you still retain default androidx startup capabilities. Indeed, docs for
koin-androidx-workmanager
are confusing and don't reveal to me what I'd gain if I use it.
c
The way I understand it (and maybe I'm wrong) is that Koin (or Hilt) offer specific injection features for those components that the user has no control over construction - i.e, that the system constructs for you. Examples of this are ViewModel and WorkManager Workers. Both these components have some sort of Factory API that you should be using if you want to pass dependencies while constructing them. Koin (and Hilt) offer specific support for these components that helps you hide the Factory boilerplate from you.
WorkManager.getInstance()
is just to get an instance of WorkManager. Koin's WM integration allows you to declare and inject
Worker
instances, not just the
WorkManager
instance. It allows you to do this:
Copy code
worker {
    FirmwareUpdateWorker(
        bleRepository = get(),
        appContext = get(),
        firmwareUpdateConfig = get(),
        params = it.get()
    )
}
Here.
FirmwareUpdateWorker
sub-classes
CoroutineWorker
and it has constructor parameters that Koin helps you inject.
Elsewhere when you use say
val firmwareUpdateWorkRequest = OneTimeWorkRequestBuilder<FirmwareUpdateWorker> ()
Koin's
workerManagerFactory
and the above
worker
DSL work together to obtain the correct instance of the Worker.
At least that's my understanding. Please correct me if I'm wrong!
a
I see, that would help reduce boilerplate a bit, yeah. Right now I've gotten used to
getKoin().inject
inside the worker.
118 Views