rxjava线程切换主线程_Android中的rxjava多线程

rxjava线程切换主线程

重点 (Top highlight)

RxJava has been in the development market for a long time but while interacting with some of my colleagues, I found out that many people haven’t started using RxJava because they are unaware of the benefits it provides.

RxJava进入开发市场已经很长时间了,但是在与我的一些同事进行交互时,我发现许多人尚未开始使用RxJava,因为他们没有意识到RxJava所提供的好处。

Although there are many articles, I am writing this post to explain how easily one can get accustomed to using RxJava. At the starting point, everything is new, like the terminology and what to use.

尽管有很多文章,但是我写这篇文章是为了解释人们如何习惯使用RxJava。 从一开始,一切都是新的,例如术语和使用方法。

We need to continue working with it so we get habituated and understand it. In this post, we will discuss the basics of RxJava and some useful insights.

我们需要继续使用它,以便习惯它并了解它。 在本文中,我们将讨论RxJava的基础知识和一些有用的见解。

什么是RxJava (What Is RxJava)

RxJava is a Java VM implementation of Reactive Extensions: a library for composing asynchronous and event-based programs by using observable sequences.

RxJava是Reactive Extensions的Java VM实现:该库用于通过使用可观察的序列来组成异步和基于事件的程序。

Simply, we can define it as an API for asynchronous programming with observable streams. It is a combination of the best ideas from the observer pattern, the iterator pattern, and functional programming.

简单来说,我们可以将其定义为用于具有可观察流的异步编程的API。 它是来自观察者模式,迭代器模式和函数式编程的最佳创意的组合。

It extends the observer pattern to support sequences of data/events and adds operators that allow you to compose sequences together declaratively while abstracting away concerns about things like low-level threading, synchronization, thread-safety, and concurrent data structures.

它扩展了观察者模式以支持数据/事件序列,并添加了运算符,使您可以声明性地将序列组合在一起,同时消除了对诸如低级线程,同步,线程安全性和并发数据结构之类的问题的担忧。

In other words, we can say that data emitted by one component and the underlying structure provided by the Rx libraries will propagate those changes to another component that is registered to receive those data changes.

换句话说,我们可以说一个组件发出的数据和Rx库提供的基础结构会将这些更改传播到另一个组件,该组件已注册以接收这些数据更改。

为什么要使用RxJava? (Why Should We Use RxJava?)

Before starting to use anything, we need to analyze why we need this thing. Using the Executor Service for threading and AsyncTask for other asynchronous operations can perform our task but we have a lot more pain to manage them properly.

在开始使用任何东西之前,我们需要分析为什么需要这个东西。 使用Executor Service进行线程处理,使用AsyncTask进行其他异步操作可以执行我们的任务,但是要正确地管理它们,要付出更多的努力。

简单的多线程 (Easy multi-threading)

For asynchronous work, thread management is crucial. In many situations, while a task is being performed, we encounter a situation of communicating between a background thread and the main thread.

对于异步工作,线程管理至关重要。 在许多情况下,当执行任务时,我们会遇到在后台线程和主线程之间进行通信的情况。

A simple example is updating the UI while the background thread is being executed. Although it sounds easy, we know how many checks we need to perform before updating the UI.

一个简单的示例是在执行后台线程时更新UI。 尽管听起来很简单,但我们知道在更新UI之前需要执行多少检查。

Rx has made it easier for us so with the use of Rx, we can control the task that we started in a separate thread easily.

Rx简化了我们的工作,因此使用Rx,我们可以轻松控制在单独线程中启动的任务。

简单的错误处理 (Simple error handling)

Errors are the most frustrating thing for developers. While performing a lot of complex, asynchronous operations, we encounter errors in many places.

对于开发人员而言,错误是最令人沮丧的事情。 在执行许多复杂的异步操作时,我们在许多地方遇到错误。

To avoid this we generally use try/catch or some other patchy code to resolve this. But Rx provides a standard approach for handling these errors. It has a standard success and failure delivery approach.

为了避免这种情况,我们通常使用try / catch或其他一些修补代码来解决此问题。 但是Rx提供了处理这些错误的标准方法。 它具有标准的成功和失败交付方法。

避免回调地狱 (Avoid the callback hell)

At any point, if you have made nested network calls, you may know what the pain will be of handling them. But Rx has many operators to resolve these kinds of issues very easily.

在任何时候,如果您进行了嵌套的网络调用,您可能都知道处理它们的痛苦。 但是Rx有许多运营商可以

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值