RxSwift: WithLatestFrom

Muhammad Muizzsuddin
2 min readFeb 5, 2018

--

[If you are English speaker, or at least decent in English language. Please help me to write a good English article]

[If you know how to write a good article, let me to know :D]

Send me a blank mail? m.muizzsuddin@gmail.com

Today I am going to share my latest learning material and understanding, not complete yet, but it’s worth to read.

The case I am against is my User publish subject is observed by two observer. The former is to build a cart observable which will be consumed by the latter. Seems to be just a cyclic observable, right?

I want you to know that I am not to teach you how to use RxSwift operators properly in the first place. I am learning this new concept of Functional Reactive Programming for my self. I am working on a project which the architecture drive me to learn a lot about MVVM+Coordinator+RxSwift. A daunting task for the first time but later it become very amusing to know how operators work on their behalf, not to depend each other. I am ready to know more about functional programming such as Haskell.

The operator withLatestFrom is provided to leverage the ability of the Observable sequence to catch latest sequence emitted by certain Observable source. Which in my case is very helpful since I need to ensure the observable sequence I’ve build not make a cyclic sequence.

Here is the situation that comes to me.

  1. User Observable emitting value that converted to Cart Observable. Cart is a property of User. I am using flatMapLatest operator here.
  2. Cart Observable updated based on recent activity.
  3. Now CartObservable is used to update carts in user
  4. I am using Value types here so then I send updated User to the User publish Subject.
  5. Because I am using combineLatest operator, User now emit new value while the former value emitted isn’t completed yet. That drives the Rx warn me with “Reeentrancy anomaly”.
  6. How to tackle the warning?
  7. Yups. Use the withLatestForm operator. I just interested to the change in User Observable source whenever I need it. No more. So the “drive” of the sequence isn’t the User anymore.

That’s all. This is just a note. So tell me how to write a good article, eh?

The Code you’ve been missing. :D

--

--

No responses yet