This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public void login(final String mac, final String devicesOs, final String appVersion, String userAcct, String userPwd, String memType) { | |
getSubscription().add(ApiClient.getInstance().personLogin(mac, devicesOs, appVersion, userAcct, userPwd, memType) | |
.doOnSubscribe(new Action0() { | |
@Override | |
public void call() { | |
getView().showLoading(); | |
} | |
}) | |
.subscribeOn(Schedulers.newThread()) | |
.observeOn(AndroidSchedulers.mainThread()) // (a) | |
.flatMap(new Func1<PersonLogin, Observable<PersonData>>() { | |
@Override | |
public Observable<PersonData> call(PersonLogin personLogin) { | |
// 此處完全不會call (下Log並沒有call) | |
// 我想在此就處理Ui所以切AndroidSchedulers.mainThread() (a)處 | |
// return 這個Api又必須切到新的Thread去做 所以 (1)處call.subscribeOn(Schedulers.newThread()) | |
return ApiClient.getInstance().queryPersonData(mac, devicesOs, appVersion, personLogin.getUId(), personLogin.getSessionId()); | |
} | |
}) | |
.subscribeOn(Schedulers.newThread()) // (1) | |
.observeOn(AndroidSchedulers.mainThread()) // (2) | |
.subscribe(new Observer<PersonData>() { | |
@Override | |
public void onCompleted() { | |
} | |
@Override | |
public void onError(Throwable e) { | |
// 此處拿到 main thread | |
Log.d(TAG, Thread.currentThread().getName()); | |
// 如果call 如 Toast or e.getMessage(); | |
// 此處噴 Can't create handler inside thread that has not called Looper.prepare() | |
} | |
@Override | |
public void onNext(PersonData personData) { | |
// 我想在此處也處理Ui所以切AndroidSchedulers.mainThread() (2)處 | |
} | |
})); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment