This article is a continuation of My RxSwift Summary ①.
There are two points to be aware of in asynchronous processing.
--Code execution order --How to handle shared mutable data
Is.
RxSwift addresses these issues by incorporating the following two concepts:
-Declarative programming → Example: If you define ʻa = b + c, the value of ʻa
will be b
and c
. Update every time the value changes.
-[Functional Programming](https://qiita.com/tail-island/items/1782e4c1e7b620a4f9c0#%E9%96%A2%E6%95%B0%E5%9E%8B%E3%82%82%E3% 81% 84% E3% 81% 84% E3% 81% 91% E3% 81% A9% E3% 82% AA% E3% 83% 96% E3% 82% B8% E3% 82% A7% E3% 82% AF% E3% 83% 88% E6% 8C% 87% E5% 90% 91% E3% 82% 82% E3% 81% AD) → A method of describing processing by a series of function input / output
And I have the following five features. (= [Reactive System](https://thinkit.co.jp/article/9185#:~:text=%E3%83%AA%E3%82%A2%E3%82%AF%E3%83%86 % E3% 82% A3% E3% 83% 96% E3% 82% B7% E3% 82% B9% E3% 83% 86% E3% 83% A0% E3% 81% A8% E3% 81% AF% E3 % 80% 81% E3% 81% B2% E3% 81% A8% E3% 81% 93% E3% 81% A8% E3% 81% A7% E8% A8% 80% E3% 81% 86% E3% 81 % A8% E3% 80% 8C% E5% 8D% B3% E5% BF% 9C,% E3% 80% 8D% E3% 81% A7% E3% 81% 99% EF% BC% 88% E5% 9B% B31% EF% BC% 89% E3% 80% 82)))
--Responsive → Always reflect the latest state of the application in the UI --Resilient → Each process is separated and error recovery is easy. --Elastic → Lazy loading for Variable workload % E9% 81% 85% E5% BB% B6% E8% AA% AD% E3% 81% BF% E8% BE% BC% E3% 81% BF) and [Throttle](https: // future-architect) .github.io/articles/20200121/), resource sharing and other features --Message-driven → Communicate between components asynchronously using message-based communication, loosely coupled for better reusability, and implemented separately from the class lifecycle
RxSwift has three components: ʻObservable / operator / scheduler`.
Observable
An observable object defined by ʻObservable
Multiple ʻobservers can react to events in real time to update the UI and use data. ʻObservableType protocol
can generate the following three events.
--next
→ Event that brings the next data to ʻobserver. Keep bringing the value to ʻobserver
until completed
occurs.
--completed
→ End a series of events with success
and notify the observer.
--ʻError → Notify that ʻobservable
ended with ʻerror`.
Operators
ʻObservable contains a number of methods (ʻOperators
) that perform asynchronous, event-based processing. Since these produce only the output without the Side effect (which is reflected in the UI on the user side), you can combine ʻOperator to convert the input to any value. To name a typical ʻOperator
,
--filter
→ Extract only the values that meet the conditions
--Perform processing for all values that flowed in map
→ ʻObservable --
skip` → Skip a specific value
And so on. (I'll summarize it in another article later.)
Scehduler
Scheduler
is similar to Dispatch queue
, and processing can be divided into main thread and sub thread. RxSwift has a lot of predefined Scheduler
s, so it's convenient!
RxCocoa
RxSwift
is about common use of Rx, not just Swift
. UIKit
etc. peculiar to Swift
will be handled using RxCocoa
.
RxCocoa
is a library that adds reactive functionality to many ʻUI parts`.