简体   繁体   English

我应该在哪里订阅 MVVM android 应用程序中的 Rxjava?

[英]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() {}
});

I'm just getting started with MVVM architecture and Rxjava, I don't know where should I place the above code (use to sign up a user).我刚刚开始使用 MVVM 架构和 Rxjava,我不知道我应该将上面的代码放在哪里(用于注册用户)。 Should be ViewModel, Activity, or any other place?应该是 ViewModel、Activity 还是其他任何地方? I want to show the result to user after getting the it.我想在获得结果后向用户显示结果。

You should subscribe this method in ViewModel because it has orientation independent lifecycle.您应该在 ViewModel 中订阅此方法,因为它具有与方向无关的生命周期。 You can dispose in onCleared method of view model to to stop background operation.您可以在视图 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. 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM