Reputation: 1927
I was looking at the example of an android app coded in MVP (link here). But now I want to recode the given RxJava1 code in the tutorial to an RxJava2 code. However, I am having trouble with it especially unSubscribe()
and isUnSubscribed()
. I tried to convert it and I will share my attempt.
RxJava1 Code:
public void doLogin(AuthCredentials credentials) {
cancelSubscription();
subscriber = new Subscriber<Account>() {
@Override public void onCompleted() {
if (isViewAttached()) {
getView().loginSuccessful();
}
}
@Override public void onError(Throwable e) {
if (isViewAttached()) {
getView().showError();
}
}
@Override public void onNext(Account account) {
eventBus.post(new LoginSuccessfulEvent(account));
}
};
// do login
accountManager.doLogin(credentials)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(subscriber);
}
private void cancelSubscription() {
if (subscriber != null && !subscriber.isUnsubscribed()) {
subscriber.unsubscribe();
}
}
This is my attempt on RxJava2 Code:
public void doLogin(AuthCredentials credentials) {
cancelSubscription();
subscriber = new Subscriber<Account>() {
@Override public void onSubscribe(Subscription s) {
// do login
accountManager.doLogin(credentials)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(subscriber);
}
@Override public void onCompleted() {
if (isViewAttached()) {
getView().loginSuccessful();
}
}
@Override public void onError(Throwable e) {
if (isViewAttached()) {
getView().showError();
}
}
@Override public void onNext(Account account) {
eventBus.post(new LoginSuccessfulEvent(account));
}
};
}
private void cancelSubscription() {
//isUnsubscribed and unsubscribe doesnt work anymore
}
I am fairly new to the concept of RxJava. If anyone can point out my mistakes and guide me that would be great. :)
Upvotes: 2
Views: 480
Reputation: 1927
I apologize for the late answer. Have been extremely busy. As stated by @akarnokd there have been made a lot of changes in RxJava2 as compared to RxJava1. For those who interested please look at this video.
As far as the above question is concerned, we can achieve the same by using DisposableObservables
Here is the answer to the above question. I have tested it and it works.
public void doLogin(AuthCredentials credentials) {
myDisposableObserver = new DisposableObserver<Account>() {
@Override
public void onNext(Account account) {
eventBus.post(new LoginSuccessfulEvent(account));
}
@Override
public void onError(Throwable e) {
if (isViewAttached()) {
getView().showError();
}
}
@Override
public void onComplete() {
if (isViewAttached()) {
getView().loginSuccessful();
}
}
};
// do login
accountManager.doLogin(credentials)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(subscriber);
}
}
private void cancelSubscriptionToMyPrescriptorManager() {
if (myDisposableObserver != null && !myDisposableObserver.isDisposed()) {
myDisposableObserver.dispose();
}
}
Upvotes: 2