[英]Where should I subscribe with Rxjava in MVVM android app?
LCUser user = new LCUser();
...
user.signUpInBackground().subscribe(new Observer<LCUser>() {
public void onSubscribe(Disposable disposable) {}
public void onNext(LCUser user) {
// Sign up Successful
}
public void onError(Throwable throwable) {
// Sign up Failed
}
public void onComplete() {}
});
我剛剛開始使用 MVVM 架構和 Rxjava,我不知道我應該將上面的代碼放在哪里(用於注冊用戶)。 應該是 ViewModel、Activity 還是其他任何地方? 我想在獲得結果后向用戶顯示結果。
您應該在 ViewModel 中訂閱此方法,因為它具有與方向無關的生命周期。 您可以在視圖 model 的 onCleared 方法中配置以停止后台操作。 It's best practice too, subscribing Rx subscriber in view model, and for coroutine to launch in view model in view model scope.
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.