rxCocoa Flashcards
Rules for RxCocoa traits
They can’t error out.
They are observed on main scheduler.
They subscribe on main scheduler.
They share resources (e.g. share(replay: 1)), except for Signal.
RxCocoa traits
ControlProperty & ControlEvent
Driver
Signal
Driver vs Signal
Signal is useful for modeling events, where a Driver is more suitable for modeling state.
“You might consider this trait as an alternative to Driver, but there’s an important detail you have to consider first: there’s no replay of the last event after subscription.
The difference between Driver and Signal is a bit like between BehaviorSubject and PublishSubject. After you’ve written RxSwift code for a while, you usually figure out the nuances of when to use which.
To help you decide which to use, simply ask yourself: “Do I need a replay of the last event when I connect to the resource?”
If your answer is no, then Signal is a good option; otherwise, Driver is the solution.
Driver vs Bind(to:)
drive works quite similarly to bind(to:); the difference in the name better expresses the intent while using RxCocoa’s Traits
How to create flow api -> business logic -> UI
Divide api calls
Merge them
merge -> map -> drive
Create flows that you needed for UI components
How to create .rx. extension for component?
Create Delegate Proxy
Add ControlProperty & ControlEvent that you needed
Forward proxy vs Delegate proxy
Forward proxy is just normal component delegate that you have to set manually on view controller.
It’s useful when you want delegate directly from component without rx
You can use Delegate Proxy and Forward Proxy
It’s useful for delegates that return values for customisation
When I can use [unowned self]?
When use the same observable from transforming parameters you can use [unowned self]
let mapInput = mapView.rx.regionDidChangeAnimated .skip(1) .map { [unowned self] _ in self.mapView.centerCoordinate }
bind(to:) is actually
bind(to:) is actually an alias, or syntactic sugar, for subscribe(). Calling bind(to: observer) will internally call subscribe(observer). The former is simply in place to create a more meaningful and intuitive syntax.