在Android中使用rxjava的原因

One of the main challenges in implementing a robust software particularly an Android app is facing with the dynamic nature of changing inputs. Traditional asynchronous programming models want to rely on callbacks to update these changes, but these methods have some negative consequences. Therefore, RxJava as a popular library based on Reactive Programming concepts has addressed this matter effectively and successfully. This essay aims to discuss some reasons for using RxJava in Android in order to increase performance and productivity.

实施功能强大的软件(尤其是Android应用)的主要挑战之一是更改输入的动态性。 传统的异步编程模型希望依靠回调来更新这些更改,但是这些方法会带来一些负面影响。 因此,RxJava作为基于React式编程概念的流行库,已成功有效地解决了此问题。 本文旨在讨论在Android中使用RxJava来提高性能和生产率的一些原因。

什么是RxJava和React式编程? (What is RxJava and Reactive Programming?)

Basically, RxJava is a library for composing asynchronous streams of real-time data and event-based programs by using observable sequences in a Reactive Programming style.

基本上,RxJava是一个库,用于通过以可响应编程方式使用可观察的序列来组成实时数据和基于事件的程序的异步流。

In Reactive Programming the consumer reacts to the data as it comes in. So, This is the reason why asynchronous programming is also called reactive programming. Basically, Reactive Programming allows to propagates changing in evens to their observers. For instance, in the following equation: X = Y +Z, when we want to change the value of Y or Z, the value of X automatically changes. This change can be accomplished by observing the values of Y and Z at the same time.

在React式编程中,使用者对输入的数据做出React。因此,这就是为什么异步编程也称为React式编程的原因。 基本上,React式编程允许将变化均匀地传播给他们的观察者。 例如,在以下等式中:X = Y + Z,当我们想要更改Y或Z的值时,X的值会自动更改。 可以通过同时观察Y和Z的值来完成此更改。

Eventually, RxJava is the Java implementation of the concept of Reactive Programming in RxJava and Android to react to changes in applications.

最终,RxJava是RxJava和Android中响应式编程概念的Java实现,以响应应用程序中的更改。

RxJava如何工作? (How does RxJava work?)

As a matter of fact, RxJava extends the Observer software design pattern, which is based on the concept of Observers and Observables. If you want to work with RxJava library, you will see some concepts that play essential roles in implementation. Thus, understanding these concepts are necessary. They are mentioned briefly as follows:

实际上,RxJava扩展了基于Observers和Observables概念的Observer软件设计模式。 如果您想使用RxJava库,您将看到一些在实现中起重要作用的概念。 因此,理解这些概念是必要的。 简要介绍如下:

Observable

可观察的

Observable is a class that emits a stream of data or events.

可观察的是一个发出数据或事件流的类。

Observer

观察者

Observer is the opposite side of Observable. In fact, it is a class, which receives the events or data and also act upon it.

观察者是可观察者的反面。 实际上,它是一个类,它接收事件或数据并对其执行操作。

Subscribe

订阅

Subscribe is the bridge between Observable and Observe. Also, it is possible to be existed multiple Observers that are subscribed to a single Observable.

订阅是可观察和可观察之间的桥梁。 同样,可能存在多个订阅单个Observable的Observer。

Operators

经营者

RxJava Operators allow you to manipulate the data emitted by Observable. Basically, the Operators are the operations that allow us to change the emission before consuming such as Just, Map, From, and so on.

RxJava运算符允许您操纵Observable发出的数据。 基本上,运算符是允许我们在使用前更改发射量的操作,例如Just,Map,From等。

Scheduler

排程器

Scheduler is an important element in RxJava, which tells observable and observer for which thread should be started. In short, it manages concurrency.

调度程序是RxJava中的重要元素,它告诉可观察者和观察者应为其启动哪个线程。 简而言之,它管理并发。

In addition, there are primarily three kinds of events that your observer can receive are mentioned as follows:

此外,观察者可以接收的事件主要有以下三种:

onNext(): this method is called when a new item is emitted by Observable.

onNext() Observable发出新项目时调用此方法。

onError(): this method is called when an error occurs, and the emission of data is not successful.

onError() 发生错误且数据发送不成功时,将调用此方法。

onComplete(): this method is called when the Observable has successfully completed the emitting of all items.

onComplete() 当Observable成功完成所有项目的发射时,将调用此方法。

创建RxJava的简单步骤 (Simple steps to create RxJava)

All in all, to implement RxJava concepts, you need to have and implement some steps at least as follows:

总而言之,要实现RxJava概念,您需要至少具有并实现一些步骤,如下所示:

  1. Create an Observable.

    创建一个可观察的。
  2. Give the Observable some data to emit.

    给Observable发出一些数据。
  3. Create an Observer.

    创建一个观察者。
  4. Subscribe the Observer to the Observable.

    将观察者订阅到可观察者。

For instance:

例如:

Observable.just("x", "y", "z").subscribe(new Observer<String>() {
@Overridepublic void onNext(String s){System.out.println("onNext: " + s); }
@Overridepublic void onCompleted() { System.out.println("Completed!"); }
@Overridepublic void onError(Throwable e) {... }});

使用RxJava的好处 (Benefits of using RxJava)

  1. Having responsive applications

    具有响应式应用程序

The RxJava library is designed that gives a control over a wide range of data and events simultaneously as a real-time task. It means, RxJava can be able to do some tasks at the same time immediately such as calling multiple web services in the background, error-handling, and view-handling without decreasing the responsiveness of an Android app.

RxJava库旨在作为实时任务同时提供对多种数据和事件的控制。 这意味着RxJava能够立即同时执行一些任务,例如在后台调用多个Web服务,错误处理和视图处理,而不会降低Android应用程序的响应速度。

2. Easy Threading and Multi-threading

2. 轻松线程化和多线程化

Basically, though RxJava is single-threaded by default, it provides you to define more threading models for both background and callback tasks. Also, for doing asynchronous tasks, thread management would be essential. In many conditions, when a task is performed, we would face communication between a background thread and the main thread. For instance, updating the UI while the background thread is being executed. So, in these cases it will require a number of checking before updating the UI. However, RxJava makes it easy to implement multi-threaded and concurrent applications by using some Operators and Schedulers efficiently.

基本上,尽管RxJava默认情况下是单线程的,但它为您提供了更多用于后台和回调任务的线程模型。 同样,对于执行异步任务,线程管理将是必不可少的。 在许多情况下,执行任务时,我们将面对后台线程和主线程之间的通信。 例如,在执行后台线程时更新UI。 因此,在这些情况下,在更新UI之前将需要进行大量检查。 但是,RxJava通过有效地使用某些运算符和调度程序使实现多线程和并发应用程序变得容易。

3. Solving the problem of Callback Hell

3. 解决回调地狱的问题

Traditional asynchronous programming methods tend to rely on callbacks to update input changes, but this way can cause to a problem known as callback hell. Callback hell includes multiple nested callbacks, which make code hard to read and debug. So, one of the main consequences of RxJava is solving the problem of Callback Hell indeed.

传统的异步编程方法倾向于依靠回调来更新输入更改,但是这种方式可能会导致称为回调地狱的问题。 回调地狱包括多个嵌套的回调,这使代码难以阅读和调试。 因此,RxJava的主要后果之一就是确实解决了回调地狱的问题。

4. Higher flexibility

4. 更高的灵活性

Basically, RxJava uses a large number of Operators, which could be applied to data. It means these operators can affect the data that are emitted by Observable with various operations and behaviors. As a result, the large quantity of the operators can increase your flexibility for implementing your needs in applications more easily.

基本上,RxJava使用大量的运算符,这些运算符可以应用于数据。 这意味着这些运算符可以通过各种操作和行为来影响Observable发出的数据。 结果,大量的操作员可以提高您的灵活性,以便更轻松地实现应用程序中的需求。

5. Having a mechanism for Error handling

5. 具有错误处理机制

AsyncTask often has some errors, which occur in the background thread, and they are difficult to identify them in the process of updating the UI thread by using the PostExecute() method. However, RxJava has a clear mechanism for handling errors. In a reactive stream, elements are consumed by onNext() method first, and then onComplete() method is called when the stream ends. If you face any error in onNext() method, the control will shift to the onError() method. Besides, there are other methods for handling errors when you want to use RxJava Operators such as Map or FlatMap Operators. For example: OnErrorReturn, OnErrorResumeNext, OnExceptionResumeNext, and so on.

AsyncTask通常有一些错误,这些错误发生在后台线程中,并且在使用PostExecute()方法更新UI线程的过程中很难识别它们。 但是,RxJava具有处理错误的明确机制。 在React式流中,元素首先由onNext()方法消耗,然后在流结束时调用onComplete()方法。 如果在onNext()方法中遇到任何错误,则控件将移至onError()方法。 此外,当您想使用RxJava运算符(例如Map或FlatMap运算符)时,还有其他处理错误的方法。 例如:OnErrorReturn,OnErrorResumeNext,OnExceptionResumeNext,等等。

6. Life-cycle management

6. 生命周期管理

In some cases in Android development your background task is alive, but your Activity or Fragment that is connected to that has been destroyed. So, to prevent these cases in Android lifecycle, Rxjava has some best practices like using RxLifecycle library.

在Android开发中的某些情况下,您的后台任务仍然有效,但是与之关联的Activity或Fragment已被破坏。 因此,为防止这些情况发生在Android生命周期中,Rxjava具有一些最佳实践,例如使用RxLifecycle库。

7. Providing an standard workflow for coding

7. 提供标准的编码工作流程

RxJava provides a standard workflow that is used to manage all data and events across the application like Create an Observable> Give the Observable some data to emit> Create an Observer> Subscribe the Observer to the Observable.

RxJava提供了一个标准的工作流,用于管理应用程序中的所有数据和事件,例如创建一个Observable>给Observable发送一些数据>创建一个Observer>将Observer订阅到Observable。

Thus, it means this workflow can give you a guideline for writing clean codes. As a result, productivity and readability of codes can increase in comparison with traditional methods,

因此,这意味着该工作流程可以为您提供编写干净代码的指南。 结果,与传统方法相比,代码的生产率和可读性可以提高,

8. Integration with other libraries

8. 与其他图书馆的整合

RxJava is becoming more and more popular particularly for Android developers. So, there are a number of libraries are provided for integration on it like Retrofit.

RxJava越来越受欢迎,尤其是对于Android开发人员而言。 因此,像Retrofit一样,提供了许多用于集成的库。

In conclusion, RxJava as a prominent library specially in Android application development has addressed the problem of dynamic nature of inputs in programming effectively and successfully in comparison with traditional methods. This essay considered some positive consequences of using RxJava in Android development.

综上所述 ,RxJava作为Android应用程序开发中的著名库,与传统方法相比,已有效且成功地解决了编程中输入动态性的问题。 本文考虑了在Android开发中使用RxJava的一些积极后果。

You can also read more about this topic in these links:

您还可以通过以下链接阅读有关此主题的更多信息:

翻译自: https://medium.com/kayvan-kaseb/reasons-for-using-rxjava-in-android-7ef4d83b922a

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值