Android 在旋转设备上管理多请求 rxJava [英] Android manage multi request rxJava on rotation device

查看:37
本文介绍了Android 在旋转设备上管理多请求 rxJava的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Android 应用程序上使用 MVVM,我想在设备轮换时管理请求和 rxJava,如何在轮换设备后禁用请求并从上次请求开始计算?

I'm using MVVM on android application and i want to manage requests and rxJava on device rotation, how can i disable request after rotation device and countinue from last request?

这是我的简单代码,用于了解如何执行此操作,但我找不到有关它的任何文档和示例代码

this is my simple code to know how can i doing that, but i can't find any document and sample code about it

@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    binding = DataBindingUtil.setContentView(this, R.layout.activity_register);
    ...
    Observer<String> myObserver = new Observer<String>() {
        @Override
        public void onError(Throwable e) {
            // Called when the observable encounters an error
        }

        @Override
        public void onComplete() {

        }

        @Override
        public void onSubscribe(Disposable d) {

        }

        @Override
        public void onNext(String s) {
            // Called each time the observable emits data
            Log.e("MY OBSERVER", s);
        }
    };

    Observable.just("Hello").subscribe(myObserver);
}

我使用的是最新版本的 rxJava

I'm using latest version of rxJava

推荐答案

在 Android 中处理轮换是一个很酷的挑战.有几种方法可以做到这一点.

Handling rotation is a cool challenge in Android. There're a few ways to do that.

1- 服务:您可以使用服务并在服务中处理您的网络请求或其他后台操作.同样使用服务,您可以将您的业务逻辑与 ui 分开.

1- Services: You can use a service and handle your network requests or other background operations in service. Also with Services, you'll seperate your business logic from ui.

2- Worker Fragment: Worker Fragment 是一个没有布局的 Fragment 实例.您应该将工作片段的 retainInstanceState 设置为 true.因此,您将保存您的片段免受方向更改的影响,并且不会丢失您的后台操作.

2- Worker Fragment: Worker fragment is a fragment instance without a layout. You should set your worker fragment's retainInstanceState to true. So you'll save your fragment from orientation change and will not lose your background operations.

为什么要使用 Worker Fragment?如果您将 retainInstanceState 设置为具有布局的片段,则会泄漏视图.

Why Worker Fragment? If you set retainInstanceState true to a fragment with layout, you'll leak views.

如果您使用 MVVM,您可以将 ViewModel 实现为一个 Worker Fragment,即 setRetainInstanceState = true

If you're using MVVM you can implement ViewModel as a Worker Fragment which as setRetainInstanceState = true

3- 全局单例数据源:您可以创建一个全局单例数据源类,该类在与应用程序中的 Activity/Fragment 生命周期无关的范围内处理您的操作.

3- Global Singleton Data Source: You can create a global singleton data source class which handles your operations in an independent scope from Activity / Fragment lifecycle in your application.

4- 加载器:加载器可以从方向变化中恢复状态.您使用加载程序处理您的操作,但它们旨在从磁盘加载数据,不太适合长时间运行的网络请求.

4- Loaders: Loaders can recover state from orientation changes. You handle your operations with loaders but they are designed to load data from disk and are not well suited for long-running network requests.

额外:您可以使用 Path 的优先作业队列 来持久化您的作业:https://github.com/path/android-priority-jobqueue

Extra: You can use Path's Priority Job Queue to persist your jobs: https://github.com/path/android-priority-jobqueue

编辑:您可以在不使用 Google 的新架构组件的情况下检查我的存储库以处理设备旋转.(作为我在回答中指出的 Worker Fragment 的一个例子.)https://github.com/savepopulation/bulk-action

Edit: You can check my repo for handling device rotation without using Google's new architecture components. (As an example of Worker Fragment which i pointed in my answer.) https://github.com/savepopulation/bulk-action

这篇关于Android 在旋转设备上管理多请求 rxJava的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆