【RxSwift、英文記事引用】Why RxSwift Is Worth Using in 2022

:::note info
気になる英文記事を引用して記事を書いてみました。
:::

引用元の記事は、タイトルが「Why RxSwift Is Worth Using in 2021」とあり、2021年時点で書かれたものではありますが、現在(2022年)にて記事を眺めてみます。

>RxSwift is a library for working with asynchronous code that presents events as streams of events with the ability to subscribe to them, and also allows you to apply functional programming approaches to them, which greatly simplifies working with complex sequences of asynchronous events.
The prerequisite for the emergence of such tools is that in the process of evolution, the iOS SDK presented us with a wide selection of tools for working with asynchronous events: GCD, KVO, Notification Center, the delegation pattern, and the closure mechanism.
The problem is that each of these tools requires an individual approach and a certain codebase around them, and their combination may not be very elegant.

上記の部分は、記事の序論になります。箇条書きで要約すると、

- RxSwiftは、非同期処理の為のライブラリである
- 非同期処理の複雑なsequencesを扱うことを大幅に簡略化することができる
- 非同期処理を扱うためのツール（GCD、KVO、Notification Center、the delegation pattern、the closure mechanism）の問題点は、各々のツールが個別のapproachとその周辺の特定のcodebaseを必要とし、それらの組み合わせがあまりelegantではない可能性がある

となりますでしょうか。RxSwiftの魅力を伝えるためのイントロとなっています。引き続き、引用記事を見ていきます。

>A big plus in RxSwift is the linear recording of asynchronous operations. The examples above follow approximately the same recorded scheme:
>- We have a block of code doing useful work;
>- There is the Observable himself;
>- There is a subscribe method that binds an Observable to a specific Observer;
>- Optionally, there may be a block for transforming the source material Observable;
The method is free from memory leaks, for now we accept it as a mandatory or stylistic feature of RxSwift.
If you look at other platforms from Apple, then you can come across a large set of different APIs that Apple uses.
Data source and delegate for tables, target action for handling button clicks, subscriptions to Notification Center, KVO. Trying to use all of these APIs in one file can make the file difficult to read and maintain.
In RxSwift, with its uniform writing structure, this is a little easier. But do not forget that in RxSwift you can write a chain in such a way that only the one who wrote it can figure it out.
But on average, the same type of recording in the steps described above simplifies the maintenance of the source.
A very big plus is the fact that RxSwift is a port of ReactiveX, all knowledge can be used in other languages. This means that when creating logic using Rx, you can easily make it cross-platform and use.
Another important argument is that switching between threads is very simple, switching a heavy block of code to a background thread is easy. The main methods for managing streams are .observeOn (...) and .subscribeOn (...).
Now compared to when the Rx polarity boom just started. On the Internet, there are many examples and extensions that work well. And this is an important aspect. In my opinion, one of the most popular architectures is now MVVM.
RxSwift is perfectly compatible with MVVM. Without this framework, you would have to use third-party tools or write your own to link the View and ViewModel, and there are many ready-made solutions for this.
But even with all this, nothing is perfect and px also has big disadvantages such as redundancy. In a nutshell, RxSwift is quite redundant, you can write an application without it. In some situations it is very easy to write code with RxSwift, but the number of objects in memory, the stack of the number of method calls increases significantly.
RxSwift, alas, is not for newbies, not getting through the first minute of acquaintance without errors and redundant transformations to write the code you conceived.
Depending on the intensity, more or less confident use of the framework will take more than two weeks. But the most difficult and hindering development will be problems with debug.

〜中略〜

>RxSwift helps when you need to combine complex asynchronous chains. RxSwift also has types such as Subject, a kind of bridge between the imperative and declarative worlds.
The subject can act as an Observable, and at the same time, it can be an Observer, i.e. accept objects and issue events. And it will also be relevant if you use the MVVM architecture.

上記の部分は、記事の本論の部分になります。
中身をかいつまんで箇条書きにすると、

- RxSwiftの大きな特長は、非同期処理をリニアに記録できること
- RxSwiftは、メモリリークを起こしにくい
- APIを1つのファイルで複数使おうとすると、ファイルが読みづらくなり、メンテナンスもしづらくなる傾向があるが、書き方が統一されているRxSwiftでは、これが少し改善される
- RxSwiftがReactiveXの移植版であり、すべての知識が他の言語でも使えるということが大きな利点
- RxSwiftはMVVMと完全に互換性がある
- RxSwiftの欠点としては、codeがかなり冗長であること

といったところでしょうか。


引用記事においてはRxSwiftの有用性ばかり並べられているように感じました。
2022年現在から見たRxSwiftの特徴としても、複雑な非同期チェーンを結合するのには役立つものの、学習コスト(特に時間コスト)が高いのが大きな課題かと思っています。
RxSwift初学者にも理解しやすい仕組みを構築することが必要なのかもしれません。

# 引用文献
- 記事タイトル：Why RxSwift Is Worth Using in 2021
- 引用元URL：https://intersog.com/blog/is-rx-still-relevant-in-modern-mobile-ios-development-today/amp/
- 最終アクセス：2022.12.09