0



I have a one background Worker which fetches list of cars from the server.
The worker is started from the onCreate of a Fragment.
I want the Fragment should be notified with the list of cars that are fetched by the Worker,using LiveData and ViewModel.


Please help
Thanks in advance

Twisha Kotecha
  • 1,082
  • 1
  • 4
  • 18
Yogesh Katkar
  • 369
  • 1
  • 4
  • 16

1 Answers1

1

You can directly observe the progress of a worker by calling getWorkInfoByIdLiveData by using the id tag when creating the worker. I'm assuming the your worker will be storing the list in some local storage which your fragment can later read to update its view.

See how to observe status of work: https://developer.android.com/topic/libraries/architecture/workmanager/how-to/states-and-observation

If you only want to show the data when view is alive (and you will be fetching list on each app start), its better to move the logic to get list of cars in a separate LiveData and ViewModel and your fragment will observe that.

Umar Hussain
  • 3,461
  • 1
  • 16
  • 38