I am quite new to Rx java observables so I am not sure when to unsubscribe. this is how my code implementation is
Observable<String> sampleObservable = Observable.just(accountNum);
Subscription sampleSub = sampleObservable.subscribeOn(Schedulers.io()).subscribe(new Action1<String>() {
@Override
public void call(String accountNum) {
try {
//call backend api
AccountResponse response = getAccountDetails(accountNum);
logger.debug("Account response :" + response.toString());
} catch (Exception e) {
logger.error(e.getMessage());
}
}
});
Can I unsubscribe at the end "sampleSub.unsubscribe()". The concern that I have here is if "getAccountDetails(accountNum)" call takes time to complete and the code unsubscribes then will it cancel/affect the "getAccountDetails(accountNum)" call? If yes, then is there a way to unsubscribe only when that call is over?
Observable<String> sampleObservable = Observable.just(accountNum);
Subscription sampleSub = sampleObservable.subscribeOn(Schedulers.io()).subscribe(new Action1<String>() {
@Override
public void call(String accountNum) {
try {
//call backend api
AccountResponse response = getAccountDetails(accountNum);
logger.debug("Account response :" + response.toString());
} catch (Exception e) {
logger.error(e.getMessage());
}
}
});
//unsubscribing
sampleSub.unsubscribe();