|
|
|
|
|
by jawngee
3795 days ago
|
|
As someone who writes some pretty hardcore UIs in Cocoa, this reactive style stuff seems applicable to a pretty narrow class of application. The more you fight Cocoa, which I believe this stuff does, the bigger the mess you'll create for yourself once you view hierarchy becomes large and filled with custom views that don't really fit what 99% of these tutorials cover. Furthermore, trying emulate "dom" diffing by replacing views, instead of updating them, as they seem to be illustrating towards the end of the article (could be reading this wrong though) is pretty inefficient way of doing things in Cocoa. |
|
1. Apps are more than views.
2. ReactiveCocoa and RxSwift are just asynchronous data over time, represented as Just Another Data Structure (cough monad). Even if you just use them instead of callback blocks, it's an improvement because they can be composed, stored in data structures, and used in ways that methods taking a callback function and returning Void cannot. Cancellation is then implicit on deallocation, instead of needing to reference a "cancellation token" or other icky state like that.
3. Complex UIs are exactly where you want this type of system, because it provides type safety and compiler verification, instead of hoping that your target/selectors and KVO work and never break when you edit something and the compiler doesn't complain.
I think that a popular JavaScript UI library being named "React" has damaged the perception of FRP-like systems. Generic FRP-like systems are not related to UI, other than that they can be used for it. People are confusing "DOM diffing" for having anything to do with signals/streams.
In UIs, FRP-like systems allow you to take some evil imperative state (a slider was moved!), lift it into a happy pure-functional world, process the inputs, and drop out of evil imperative state at the end ("update the color"). For example, an RGB slider (using a bit of shorthand in places):
In four lines, we accept input from three different controls, wait for the changes to "settle" (let's say updating colorView.color is expensive for some reason), and update the view! It's very easy. Let's say we want to make a change, and only update colorView when the user taps a button: Only one change necessary. In plain-old-Cocoa, this would require another instance method to be defined.