1

I am new in retrofit/rxjava-android

Someone told me that, it is best practice if I will cancel the request if the call is not yet finished and the user leaves the activity page.

I am having problem where/how to cancel it.

Here's my code, it is working properly.

Observable<List<MyObject>> call;
public void getStaticMessages() {
    call = restInterface.loginURL();
    call.subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread()).subscribe(new Observer<List<MyObject>>() {
        @Override
        public void onCompleted() {

        }

        @Override
        public void onError(Throwable e) {
            Log.d("LOGGER", "error");
        }

        @Override
        public void onNext(List<MyObject> myObjects) {
            Log.d("LOGGER", "succcess");
        }
    });

}

1 Answer 1

1

One of the best practice is to create subscription/disposable when onStart() method of activity/fragment is called and unsubscribe /dispose when onStop called.

You can create one disposable Disposable disposable = call.subscribeOn and dispose it via disposable.dispose() or use CompositeDisposable.

I used the same approach with CompositeDisposable in one of my previous pet projects - link

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.