Improvements of flatMap function in RxSwift

How to be happy with RxSwift and many closures

Hey! If you read it, you probably good in RxSwift and ARC, understand differences between weak and unowned keywords. If no, please get acquainted with it and come back!

In my first post I want to tell you about my flatMap improvements. I start immediately with problem…

For example we have two requests: Login and LoadPosts. After login I want to get posts for current user:

So, here compiler will swear us, that we have to use ! and ? for self. Of course, you may say: “Why don’t we use unowned keyword?” Yes, we can, but I think it’s wrong to use it in that case.

The unowned or weak discussion boils down to a question of lifetime of the variable and the closure that references it.

So, there is a big article about choosing between unowned and weak: https://www.uraimo.com/2016/10/27/unowned-or-weak-lifetime-and-performance/

Weak, I Choose You! Now you probably want to change previously code to this one:

but when you have many flatMap functions it looks so messy — we don’t want to see messy code! Only elegant, beautiful code!

It’s time for awesome extension:

and now our requests look so: