Search code examples
androidkotlinandroid-architecture-componentsandroid-livedataandroid-viewmodel

LiveData is observed multiple times inside onClickListener in Android


I have a repository setup like this

class ServerTimeRepo @Inject constructor(private val retrofit: Retrofit){
    var liveDataTime = MutableLiveData<TimeResponse>()
    fun getServerTime(): LiveData<TimeResponse> {
        val serverTimeService:ServerTimeService = retrofit.create(ServerTimeService::class.java)
        val obs = serverTimeService.getServerTime()
        obs.subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).unsubscribeOn(Schedulers.io())
                .subscribe(object : Observer<Response<TimeResponse>> {
                    override fun onComplete() {

                    }

                    override fun onSubscribe(d: Disposable) {

                    }

                    override fun onNext(t: Response<TimeResponse>) {
                        val gson = Gson()
                        val json: String?
                        val code = t.code()
                        val cs = code.toString()
                        if (!cs.equals("200")) {
                            json = t.errorBody()!!.string()
                            val userError = gson.fromJson(json, Error::class.java)
                        } else {

                            liveDataTime.value = t.body()
                        }

                    }

                    override fun onError(e: Throwable) {

                    }

                })
        return liveDataTime
    }
}

Then I have a viewmodel calling this repo like this

class ServerTimeViewModel @Inject constructor(private val serverTimeRepo: ServerTimeRepo):ViewModel() {

    fun getServerTime(): LiveData<TimeResponse> {
        return serverTimeRepo.getServerTime()
    }
}

Then I have an activity where I have an onClickListener where I am observing the livedata, like this

tvPWStart.setOnClickListener {
         val stlv=  serverTimeViewModel.getServerTime()

            stlv.observe(this@HomeScreenActivity, Observer {
                //this is getting called multiple times??

            })
        }

I don't know what's wrong in this. Can anyone point me in the right direction? Thanks.


Solution

  • Issue is that every time your ClickListener gets fired, you observe LiveData again and again. So, you can solve that problem by following solution :

    1. Take a MutableLiveData object inside your ViewModel privately & Observe it as LiveData.

      class ServerTimeViewModel @Inject constructor(private val serverTimeRepo: ServerTimeRepo):ViewModel() {
      
          private val serverTimeData = MutableLiveData<TimeResponse>() // We make private variable so that UI/View can't modify directly
      
          fun getServerTime() {
              serverTimeData.value = serverTimeRepo.getServerTime().value // Rather than returning LiveData, we set value to our local MutableLiveData
          }
      
          fun observeServerTime(): LiveData<TimeResponse> {
              return serverTimeData //Here we expose our MutableLiveData as LiveData to avoid modification from UI/View
          }
      }
      
    2. Now, we observe this LiveData directly outside of ClickListener and we just call API method from button click like below :

      //Assuming that this code is inside onCreate() of your Activity/Fragment
      //first we observe our LiveData
      serverTimeViewModel.observeServerTime().observe(this@HomeScreenActivity, Observer {
              //In such case, we won't observe multiple LiveData but one
      })
      //Then during our ClickListener, we just do API method call without any callback.
      tvPWStart.setOnClickListener {
          serverTimeViewModel.getServerTime()
      }