2021年3月27日星期六

What happens when an expensive operation is happening and thread freezes for longer than RxJava Interval period

I am using RxJava Interval Like this

  compositeDisposable.add(                  Observable.interval(1, TimeUnit.SECONDS)                          .flatMap(tick -> {                              return Observable.just(doWork(tick));                          })                          .subscribeOn(Schedulers.io())                          .observeOn(AndroidSchedulers.mainThread())                          .subscribe(this::updateUI, e -> {                              disableIndicator();                              FirebaseCrashlytics.getInstance().recordException(e);                          })          );    public Status init(long tick) {      // Doing heavy task      // Here the Status is an Enum class      return output  }    public void updateUI(Status value) {      // Updates UI accordingly  }  

Everything works as expected, but I have a concern. So my doWork() method is doing some heavy task in bg and I am updating the UI in updateUI() method based on the result produced by that method.I am calling the doWork() every second using RxJava Interval. Usually it takes less than a second to execute the doWork() method. So I was just wondering what happens if I have lots of data and the doWork() method takes more than one seconds to execute, lets say it takes 2 seconds.But the interval will keep calling the doWork() method every second. Since my previous execution will take another one second to complete what will I get as return value?

https://stackoverflow.com/questions/66838060/what-happens-when-an-expensive-operation-is-happening-and-thread-freezes-for-lon March 28, 2021 at 11:08AM

没有评论:

发表评论