Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

从 RxJava 2 转向使用 Kotlin 流:多线程 #8008

Merged
merged 4 commits into from
Mar 11, 2021
Merged

从 RxJava 2 转向使用 Kotlin 流:多线程 #8008

merged 4 commits into from
Mar 11, 2021

Conversation

PassionPenguin
Copy link
Contributor

@PassionPenguin PassionPenguin commented Feb 7, 2021

resolve #7974 @lsvih ψ(`∇´)ψ

等待大佬们的校对~

@PassionPenguin
Copy link
Contributor Author

Under Review...

@PassionPenguin PassionPenguin marked this pull request as ready for review February 8, 2021 15:01
article/2021/from-rxjava-2-to-kotlin-flow-threading.md Outdated Show resolved Hide resolved
article/2021/from-rxjava-2-to-kotlin-flow-threading.md Outdated Show resolved Hide resolved
article/2021/from-rxjava-2-to-kotlin-flow-threading.md Outdated Show resolved Hide resolved
article/2021/from-rxjava-2-to-kotlin-flow-threading.md Outdated Show resolved Hide resolved
article/2021/from-rxjava-2-to-kotlin-flow-threading.md Outdated Show resolved Hide resolved
article/2021/from-rxjava-2-to-kotlin-flow-threading.md Outdated Show resolved Hide resolved
article/2021/from-rxjava-2-to-kotlin-flow-threading.md Outdated Show resolved Hide resolved
article/2021/from-rxjava-2-to-kotlin-flow-threading.md Outdated Show resolved Hide resolved
article/2021/from-rxjava-2-to-kotlin-flow-threading.md Outdated Show resolved Hide resolved
article/2021/from-rxjava-2-to-kotlin-flow-threading.md Outdated Show resolved Hide resolved
@ghost
Copy link

ghost commented Feb 24, 2021

@lsvih 校对认领

@lsvih
Copy link
Member

lsvih commented Feb 24, 2021

@zenblo 欢迎校对

For a long time RxJava was undisputed leader for reactive solutions on Android, though with Kotlin expansion and introducing cold streams (Flow) seems situation might be rapidly changing in the coming years.
Though reactive programming is not related to threading in the first place, concurrency and parallelism are very important anyway.
In this article we’ll try to make short recap on threading in RxJava 2 (with some basic caveats on its usage) and then take a look at how threading works in Kotlin Flow, so if anyone would like to migrate their code without affecting functionality it would be nice and smooth.
尽管 Kotlin 的不断扩展和冷流(Flow)的引入,可能会让未来几年的情况迅速地发生些改变,但长期以来,RxJava 一直是 Android 反应式解决方案的无可争议的领导者。虽说反应式编程最初与线程无关,但合适的线程的并发和并行对程序而言仍然非常重要。在本文中,我们将简要回顾 RxJava 2 中的线程(对其用法做一些基本说明),然后看一下 Kotlin Flow 中的线程的工作原理,让我们能在不影响功能的情况下能够顺利地完成代码迁移。
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

虽说反应式编程最初与线程无关
=>虽说响应式编程最初与线程无关


Let’s start from the short recap on RxJava 2.
让我们从对 RxJava 2 的简短回顾开始。
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

让我们从对 RxJava 2 的简短回顾开始
=>让我们先对 RxJava 2 做简短回顾

article/2021/from-rxjava-2-to-kotlin-flow-threading.md Outdated Show resolved Hide resolved
article/2021/from-rxjava-2-to-kotlin-flow-threading.md Outdated Show resolved Hide resolved

#### subscribeOn

This is an operator which declares on which scheduler observable will be subscribed to. “Where it will be subscribed” means — “on which scheduler our chain will be started”.
这是一个运算符用于声明将在哪一个调度器上设下一个 `Observable` 的订阅。“将在哪一个调度器上订阅”是指“将在哪个调度器上启动我们的执行程序”。
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这是一个运算符用于声明将在哪一个调度器上设下一个 Observable 的订阅
=>subscribeOn 是一个运算符用于声明将在哪一个调度器上设置一个 Observable 的订阅

article/2021/from-rxjava-2-to-kotlin-flow-threading.md Outdated Show resolved Hide resolved

![](https://cdn-images-1.medium.com/max/2000/1*QOMRfQTktM17z2xHUYmcrQ.png)

So in our example above, by writing `flowOn(Dispatchers.IO)` we say that we want everything **before** it run on the IO.
因此,在上面的示例中,通过编写`flowOn(Dispatchers.IO)`,我们告诉了程序我们希望在输入输出上运行执行所有代码。

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

通过编写flowOn(Dispatchers.IO)
=>通过编写 flowOn(Dispatchers.IO)

article/2021/from-rxjava-2-to-kotlin-flow-threading.md Outdated Show resolved Hide resolved
article/2021/from-rxjava-2-to-kotlin-flow-threading.md Outdated Show resolved Hide resolved
article/2021/from-rxjava-2-to-kotlin-flow-threading.md Outdated Show resolved Hide resolved
@ghost
Copy link

ghost commented Feb 25, 2021

@PassionPenguin @lsvih 文章很棒!校对完成

@PassionPenguin
Copy link
Contributor Author

@PassionPenguin @lsvih 文章很棒!校对完成

等齐人一起修改(,谢谢你的校对嗷~

@cool-summer-021
Copy link
Contributor

@lsvih 校对认领

@lsvih
Copy link
Member

lsvih commented Mar 7, 2021

@SamYu2000 欢迎校对~

@lsvih lsvih removed the 校对认领 label Mar 7, 2021
Copy link
Contributor

@cool-summer-021 cool-summer-021 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@PassionPenguin @lsvih 校对完成

article/2021/from-rxjava-2-to-kotlin-flow-threading.md Outdated Show resolved Hide resolved

> In RxJava there are also other types such as `Flowable`, `Single`, etc. In the article we’ll talk about `Observable` only because everything else applies to other types as well.
> 除了 `Observable` 以外,在 RxJava 中其实还有另外的类型,例如 `Flowable` 或是 `Single` 等。但在本文中,我们只会讲讲 `Observable`,因为它们之间是能够举一反三的。
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

因为它们之间是能够举一反三的 => 因为它们的原理是相似的。

article/2021/from-rxjava-2-to-kotlin-flow-threading.md Outdated Show resolved Hide resolved
article/2021/from-rxjava-2-to-kotlin-flow-threading.md Outdated Show resolved Hide resolved
article/2021/from-rxjava-2-to-kotlin-flow-threading.md Outdated Show resolved Hide resolved
article/2021/from-rxjava-2-to-kotlin-flow-threading.md Outdated Show resolved Hide resolved
article/2021/from-rxjava-2-to-kotlin-flow-threading.md Outdated Show resolved Hide resolved
article/2021/from-rxjava-2-to-kotlin-flow-threading.md Outdated Show resolved Hide resolved
article/2021/from-rxjava-2-to-kotlin-flow-threading.md Outdated Show resolved Hide resolved
article/2021/from-rxjava-2-to-kotlin-flow-threading.md Outdated Show resolved Hide resolved
article/2021/from-rxjava-2-to-kotlin-flow-threading.md Outdated Show resolved Hide resolved
article/2021/from-rxjava-2-to-kotlin-flow-threading.md Outdated Show resolved Hide resolved
article/2021/from-rxjava-2-to-kotlin-flow-threading.md Outdated Show resolved Hide resolved
article/2021/from-rxjava-2-to-kotlin-flow-threading.md Outdated Show resolved Hide resolved
article/2021/from-rxjava-2-to-kotlin-flow-threading.md Outdated Show resolved Hide resolved
article/2021/from-rxjava-2-to-kotlin-flow-threading.md Outdated Show resolved Hide resolved
article/2021/from-rxjava-2-to-kotlin-flow-threading.md Outdated Show resolved Hide resolved
article/2021/from-rxjava-2-to-kotlin-flow-threading.md Outdated Show resolved Hide resolved
article/2021/from-rxjava-2-to-kotlin-flow-threading.md Outdated Show resolved Hide resolved
article/2021/from-rxjava-2-to-kotlin-flow-threading.md Outdated Show resolved Hide resolved
@PassionPenguin
Copy link
Contributor Author

@lsvih 修改完啦

@lsvih lsvih merged commit a7f5bca into xitu:master Mar 11, 2021
@lsvih
Copy link
Member

lsvih commented Mar 11, 2021

@PassionPenguin 已经 merge 啦~ 快快麻溜发布到掘金然后给我发下链接,方便及时添加积分哟。

掘金翻译计划有自己的知乎专栏,你也可以投稿哈,推荐使用一个好用的插件
专栏地址:https://zhuanlan.zhihu.com/juejinfanyi

@PassionPenguin PassionPenguin deleted the translation/from-rxjava-2-to-kotlin-flow-threading branch March 11, 2021 18:29
@PassionPenguin
Copy link
Contributor Author

@lsvih 漏翻译了……

@PassionPenguin
Copy link
Contributor Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

从 RxJava 2 转向使用 Kotlin 流:多线程
3 participants