1

For those guys who are experienced in RxJava2 I have a question.

Here is my method (code simplified):

 public void getRows(){
    /*ui clear*/
    helper.getObservable(argumentRowsDependOn)
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(row -> {
                /* now, I'm showing each row to user*/
            });
}

Creating a row might be high time-consuming operation, so I'm updating UI after each emission.
Problem is that method getRows() being called multiple times, and I need to show user only last set of emissions.

Is it possible to unsubscribe from outdated emissions?
Do you have any suggestions?
Thanks in advance.

hornet2319
  • 379
  • 3
  • 15
  • Read this article https://www.androidhive.info/RxJava/android-getting-started-with-reactive-programming/. I think this article can help you. – Bek Sep 05 '18 at 09:01
  • **outdated emissions** what you man by it.According to your use case you are asking to get the latest events explain your requirement in more detail. And answer is no you can't unsubscribe few events but you can filter the data according to your business logic – Anmol Sep 05 '18 at 09:27

2 Answers2

0
private disposable: Disposable

public void getRows(){
    /*ui clear*/

if(disposable != null) disposable.dispose()

    disposable = helper.getObservable(argumentRowsDependOn)
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(row -> {
                /* now, I'm showing each row to user*/
            });
}

Something like this if you use RxJava2. Each time when you call getRows you will stop older subscription

Janusz Hain
  • 597
  • 5
  • 18
0

You can use CompositeDisposable.

Check this

 val disposables = CompositeDisposable()

 disposables.add(service.login(user)
            .observeOn(AndroidSchedulers.mainThread())
            .subscribeOn(Schedulers.io())
            .subscribeWith(object : DisposableObserver<JsonObject>() {
                override fun onComplete() {
                    //do something
                }

                override fun onNext(task: JsonObject) {
                    //do something
                }

                override fun onError(e: Throwable) {
                    e.printStackTrace()
                }
            }))
}

Then call dispose() or clear() on your situation.

If you call dispose() ,CompositeDisposable is also disposed. But If you call clear(), you can add more dispsoables in your CompositeDisposable

 override fun onDestroy() {
    super.onDestroy()

    disposables.dispose()
}
mel
  • 199
  • 1
  • 10