Hello, I'm JUN of the project with retrofit + rxandroid I have a question, so I'm asking. Haha
NetworkService service = NetworkManager.getInstance(mContext).getService();
Observable<StudyPlanGraphResData> resDataObservable = service.getStudyPlanUser(mUserStudyPlanInfo.getMbrNo(), subjectCd);
resDataObservable.subscribeOn(Schedulers.newThread()).observeOn(AndroidSchedulers.mainThread()).subscribe(new Subscriber<StudyPlanGraphResData>() {
@Override
public void onCompleted() {
}
@Override
public void onError(Throwable e) {
showNetworkPopup();
e.printStackTrace();
}
@Override
public void onNext(StudyPlanGraphResData studyPlanGraphResData) {
studyPlanGraphResData.setResult();
KumonLoadingPopup.getInstance(mContext).dismissLoading();
if (studyPlanGraphResData.isSucess()) {
makeData();
} } else {
CommonPopup errorPopup = new CommonPopup(mContext, ONEBUTTON);
errorPopup.setPopupText(studyPlanGraphResData.getResultMsg());
errorPopup.DialogShow();
}
}
});
//Data and UI operations after server communication
public void makeData(){
Error on this part
}
We have communicated as above. If an error occurs in a method called makeData() while working on UI after data processing in onNext after communication is successful, the error pop-up will appear by communication on Error. Before makeData() this.unsubscribe(); and hmm.. It's the same. I think there's something I'm missing, so please give me some advice Haha
retrofit
The reason why the subscriber's onError() is called when an error occurs in makeData() can be found in the code SafeSubscriber.java, Exceptions.java in rxJava.
SafeSubscriber.java
@Override
public void onNext(T args) {
try {
if (!done) {
actual.onNext(args);
}
} } catch (Throwable e) {
// // we handle here instead of another method so we don't add stacks to the frame
// // which can prevent it from being able to handle StackOverflow
Exceptions.throwOrReport(e, this);
}
}
Exceptions.java
public static void throwOrReport(Throwable t, Observer<?> o) {
Exceptions.throwIfFatal(t);
o.onError(t);
}
actual in the above code.Because onNext(args) is wrapped in try-catch, if makeData() encounters an error, it calls Exceptions.throwOrReport() and calls Subscriber's onError() inside the function. Therefore, onError() is invoked when an exception occurs in onNext() regardless of whether or not unsubscribe() is invoked.
If the solution is to prevent onError() from being called when an error occurs in makeData(), it would be better to make an exception with a try-catch statement inside makeData() or to modify the fundamental part where the exception occurs (if it can be improved).
I don't know if the error is occurring in makeData or onNext() because there is no error log
You said you would work on UI in makeData()
but you should always work on the main thread. Make sure that you worked on the main thread for the ui operation.
runOnUiThread(new Runnable() {
@Override
public void run() {
// UI Actions
}
});
The pop-up will only appear if it is a network error if you do the following:
@Override
public void onError(Throwable e) {
if (e instanceOf IOException)
showNetworkPopup();
e.printStackTrace();
}
© 2024 OneMinuteCode. All rights reserved.