Upgrade and Cancel

Can someone confirm if calling unSubscribe on an Observable is the right way to cancel a request?

Annoyingly, Nexus 7 issues repeated network requests, and the first call needs to be dropped because the calling activity has been destroyed.

I got around this by checking that the callback (my own) is not null before trying to use it.

Unsubscribing, however, seems like the best solution, but cannot find any information if this is the right way.

I noticed that Retrofit will throw an internal error (InterruptedException), but this did not result in the error being handled back - a good thing!

+5
source share
1 answer

As far as I know, yes, unsubscribing is the best way. If you didn’t get RxJava already installed as a dependency, I recommend you do this, this makes managing Observables much easier. RxJava is also fully compatible with Retrofit.

Then you can unsubscribe from Subscription in the onDestroy method onDestroy this:

 @Override public void onDestroy() { if (bookingSubscription != null) { bookingSubscription.unsubscribe(); } super.onDestroy(); } 

Using the onDestroy method will be useful when applied to your script. You can unsubscribe from Observables there.

0
source

Source: https://habr.com/ru/post/1212426/


All Articles