-
Notifications
You must be signed in to change notification settings - Fork 5k
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
从 RxJava 2 转向使用 Kotlin 流:多线程 #8008
Conversation
Under Review... |
@lsvih 校对认领 |
@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 中的线程的工作原理,让我们能在不影响功能的情况下能够顺利地完成代码迁移。 |
There was a problem hiding this comment.
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 的简短回顾开始。 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
让我们从对 RxJava 2 的简短回顾开始
=>让我们先对 RxJava 2 做简短回顾
|
||
#### 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` 的订阅。“将在哪一个调度器上订阅”是指“将在哪个调度器上启动我们的执行程序”。 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这是一个运算符用于声明将在哪一个调度器上设下一个 Observable
的订阅
=>subscribeOn 是一个运算符用于声明将在哪一个调度器上设置一个 Observable
的订阅
|
||
![](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)`,我们告诉了程序我们希望在输入输出上运行执行所有代码。 | ||
|
There was a problem hiding this comment.
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)
@PassionPenguin @lsvih 文章很棒!校对完成 |
等齐人一起修改(,谢谢你的校对嗷~ |
@lsvih 校对认领 |
@SamYu2000 欢迎校对~ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@PassionPenguin @lsvih 校对完成
|
||
> 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`,因为它们之间是能够举一反三的。 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
因为它们之间是能够举一反三的 => 因为它们的原理是相似的。
Co-authored-by: zenblo <[email protected]> Co-authored-by: samyu2000 <[email protected]>
@lsvih 修改完啦 |
@PassionPenguin 已经 merge 啦~ 快快麻溜发布到掘金然后给我发下链接,方便及时添加积分哟。 掘金翻译计划有自己的知乎专栏,你也可以投稿哈,推荐使用一个好用的插件。 |
@lsvih 漏翻译了…… |
resolve #7974 @lsvih ψ(`∇´)ψ
等待大佬们的校对~