question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Question] Using ViewModels in Service

See original GitHub issue

Hi,

Everything is great with ViewModel. They work as expected in the Activity and Fragment. Recently I have encountered an need to use the ViewModel in Service. I’m not sure how exactly to do that.

I found one answer on StackOverFlow: https://stackoverflow.com/questions/44708202/observe-livedata-from-foreground-service But I want to know what’s the best practice to use ViewModel in Service.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:17
  • Comments:10 (1 by maintainers)

github_iconTop GitHub Comments

74reactions
florina-muntenescucommented, Sep 7, 2017

The ViewModel should be used closely with an Activity or a Fragment, so it’s destined to live in the UI layer of your application. Therefore, I don’t recommend using the ViewModel in a Service. Create a different class, that would be used in the Service and, if needed, in the ViewModel. Like this you ensure the separation of concerns and avoid giving the ViewModel more responsibilities than needed.

6reactions
MotiBartovcommented, Feb 11, 2018

How your Service negotiate with the UI?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Access viewModel inside a service in android - Stack Overflow
The ViewModel should be used closely with an Activity or a Fragment, so it's destined to live in the UI layer of your...
Read more >
Services and ViewModels in Android MVVM - YouTube
Android : Services and ViewModels in Android MVVM - How do they interact?
Read more >
ASP.NET MVC use ViewModels with Services
I'm trying to make a simple Blog-application, but I'm a bit stuck on the architecture of the application. I'd like to create a...
Read more >
ViewModel overview - Android Developers
ViewModel lets you manage your UI's data in a lifecycle-aware fashion.
Read more >
Android View Model and How it works internally - DeHaat
ViewModel can be created with activity context or fragment context. When a ViewModel object is created, it is stored inside Activity OR ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found