is it possible to easily inject a viewmodel into a...
# koin
m
is it possible to easily inject a viewmodel into a Service? In trying to inject this via
by viewModel()
per the docs, I wind up with the following:
java.lang.ClassCastException: com.myapp.MyService cannot be cast to androidx.lifecycle.ViewModelStoreOwner
Doing
by viewModel()
inside of
Activity
and
ComponentActivivty
works fine. I also tried switching from
Service
to
LifecycleService
with no luck. Any help is appreciated! 🙂
w
🤔 Try adding the interface
KoinComponent
to your Service.
m
this was my backup plan (or just doing a bit of refactoring to rely on a different class), just found it odd that the docs say it should be just this simple 🤔
🤔 1
w
We use that with a Service and can use
Copy code
by inject()
without any problems. Don't think you will be able to use
viewModel
🤔 As your Service isn't a View, right? So why you would need that? If any logic you want is inside that ViewModel, I would say to extract into a
UseCase
and inject in your ViewModel and in your Service.
m
yeah, I agree from a "semantic" standpoint, I was just approaching it from the standpoint that a
ViewModel
as an architecture component doesnt really formally have anything to do with UI that I know of, but is nicely tied to the lifecycle of its owner
with this in mine I figured that if google went to the trouble to build
LifecycleService
it'd be worth a shot
also my particular broader requirements annoyingly require methods specific to activities so I've been having to think of creative solutions 😛 - something to revisit on monday I suppose 😄