0

I am dealing with a youtube api to fetch subscriber count of channel, now as the subscriber count change every second I need to make the retrofit call every second. I am using android Jetpack components in my application, I want to know how to update the livedata every second, i.e. making the retrofit call every second.

Currently my application works fine using a Handler, Runnable and

handler.postDelayed(this, 1000)

But this handler uses UI thread, which is not good.

I have created a repository class and view model class.

curiousgeek
  • 903
  • 11
  • 18
  • you can easily call any retrofit request from UI thread but the idea of doing that every second sounds crazy to me – pskink Dec 26 '18 at 18:20
  • @pskink do you have a better solution, i have explained my case... – curiousgeek Dec 26 '18 at 18:24
  • i already said: `"you can easily call any retrofit request from UI thread"`- this is perfectly ok to call retrofit request from UI thread – pskink Dec 26 '18 at 18:25
  • i have no much experience with retrofit `Callback`s but 5 minute googling gives [callbackExecutor](https://square.github.io/retrofit/2.x/retrofit/retrofit2/Retrofit.Builder.html#callbackExecutor-java.util.concurrent.Executor-) – pskink Dec 26 '18 at 18:34
  • Every second? You really should consider using a solution that doesn't drain the user's network quota. It is not free, you know. – EpicPandaForce Jan 04 '19 at 00:02

1 Answers1

0

You have multiple options like AsyncTask, Timer, Service, AlarmManger. Take a look at these questions:

Scheduling recurring task in Android

Send request to web service every 5 second

Ehsan Mashhadi
  • 2,568
  • 1
  • 19
  • 30