RxSwift – Popular Combining Operators

Tram Ho

The recent launch of Combine has shown that the urgency of binding for Swift . Before waiting for apple to complete it, we should still use the current RxSwift. Once you understand RxSwift, it’s easier to use Combine. In this article, I will write about the popular Combine Operators of RxSwift. To better understand its use and how it works, besides reading and practicing, you can use the RxMarbles website as a tool to make learning RxSwift easier.

CombineLatest

combineLatest: is one of the operators you will use a lot. This operator will combine the two latest items emitted by the two observables and return them to us. The important thing to note here is that in order to receive an emit item from this operator, both source observables must generate at least 1 previous item. As you can see the example here has 2 source observables: emit observables out (above) and observables emit out shapes (below). At the time of observables emit in purple, combineLatest has not emit anything yet because observables emit has the shape that does not emit any item. Because of this feature, this operator is often used when this return value depends on the mix of other Observables especially when sync data between server and client, or simply enabling a button when satisfied. condition of 2 textfields boxes. Here is an example code snippet:

Zip

zip: operator is similar to combineLatest: but zip: always creates pairs of events with the same index. Let’s look at the following example on Rxmarbles It can be seen that by the time item 1 was emit, the zip could not return anything similar to combineLatest: But the other is zip: combining items from source observables based on index . If it is combineLatest: at the time of emit item 2, at combineLatest: will get 2A as shown: But because zip: based on index to coordinate, it will find the second item emit from the observables below to coordinate. So when item B is emit immediately operators will return you the results.

WithLatestFrom

If the above two operators will emit when either of the source observables emits the event as long as the conditions of each operator are met. What if there were clearer constraints of this observables. In programming we often encounter a trigger event that happens to retrieve data, which is exactly what withLatestFrom: do. This operator will treat one observables as triggers, the other observables will source. Whenever the trigger is emit, it will return that trigger and the latest item from the source observables, if the source observables have not emitted any items, there will be no emit items from the operator. Take a look and analyze this marble: The above observables will act as triggers, the lower observables will be the source of this as if you clicked on a tableView cell. The selected cell operation will be the trigger and the data of that cell will be the source observables (and of course we always want to tap the cell to get the latest information of that cell right). Let’s analyze marble on which:

  • When trigger 1 is emit, unfortunately, no data is generated at the source observable so we will not get any results in the operator.
  • When item A is emit in source observables, unlike the other two operators it will not return results because? Because in withLatestFrom: it will be emitted only when the trigger is emit. It is not necessary to not click on the cells but already have the data returned.
  • When trigger 2 is emit, it will retrieve the latest item in the source observables now item A and the result in the operator will be 2A at this time.

summary

We realize that:

  • combineLatest: would be preferred for updating variables depending on two or more conditions.
  • withLatestFrom: will be used as a trigger, and of course is very used already.
  • zip: like combineLatest: but based on index.

References: https://medium.com/swift-india/rxswift-combining-operators-combinelatest-zip-and-withlatestfrom-521d2eca5460

Share the news now

Source : Viblo