If you are a user of RxSwift, you probably already know about RxCocoa, and you must also hear about the advantages of RxSwift such as less code, easier error handling, and simpler concurrency handling. , …. (in general a lot more things) So what is RxCocoa
, sounds like RxSwift
, why do we still need RxCocoa
when we have the divine RxSwift
? And why did Observable
spawn more concepts like Single
, Maybe
, Completable
or Driver
, … (probably to make it difficult for developers) If you have such questions in mind then perhaps this article will be helpful to you. This article will help you partly answer the above questions and partly PR for Driver
First, to answer the above question, make it clear that RxSwift
is not something divine and perfect, it is only perfect when we put it in the right place, in the right place, in the right case. RxCocoa
also built from RxSwift
and it is also part of RxSwift
, it has extra extensions to help us work with UI more easily.
Trait
OK, if you already know that they are two mutually supportive libraries, then let’s explore the concepts outlined at the beginning of what Single
, Maybe
, Completable
or Driver
are? All of them are collectively called Trait
, which defines a textbook machine can be defined as: “Trait is a struct wrapper with an Observable Sequence property inside it. Trait can be considered as an application of the Builder Pattern to Observable. ” And if you find the above definition a bit confusing, you can think that Trait
is a Observable đặc biệt
. Why is it special? because Trait
has different characteristics from Observable
- Trait does not emit an error.
- Trait is observe and subscribe on MainScheduler.
- Trait shares
Side Effect
(By the way,Side Effect
are changes outside ofscope
that do not affectscope
, for example: when you call .do (onNext: // sthg here))
This is a general concept of Trait
and of course different Trait will have different characteristics that create diversity and make it possible for us to use them in different cases. To prove the above point, we will try to learn about Single
. Single
has the property that it will emit either .success(value)
or .error
(essentially .success(value)
is a combination of .next
and .completed
.) Based on this feature, Single
can use Used in cases where the event is emitted sporadically and has only success or error, such as used to emit the event when calling the API, loading data from disk, when downloading data, … Or Completable
with features Just emit out .completed
or .error
will be suitable for tasks that you only care about when the task is complete, typically writing data to a file.
In short, Observable
helps us a lot and Trait
makes it even better. Like RxSwift
and RxCocoa
always used in parallel because they complement each other very well.
Driver
For the rest of the article, learn about Driver
, which is also considered a Trait
and is a component of RxCocoa
. Driver
no different from Observable
, only it does not emit error and it always executes on Main
. These features are really tuyệt vời
when we work with UI. Why? Because Driver was born to do it. Don’t use Observable
then subscribe
on ViewController to show Alert because you’ll probably forget to observe on MainScheduler
and it might crash. And if you use Observable as an output and listen to it then be careful because it could be duplicate
because Observable has no share
available. That is why the Driver
is suitable and extremely suitable when used with the UI.
Conclude
Through the article I hope to clarify for you the problem “Observable is born not doing everything”, in ReactiveX spawns a lot of concepts to supplement and to undertake different parts, So if we understand them, we can find the best use. The article is purely personal opinion and evaluation, so if something is not correct, I hope the “gentle” and “emotional” comment will be absorbed.