Skip to main content

Functional Reactive Programming Intro

Front-end Development
Back-end Development
Mobile Development

I have been using ReactiveCocoa for a few years now, and decided it was time to write about it. What landed me in the library the in the first place was simply wanting better KVO, particularly during form validation.  It didn't take long for me to become very curious about FRP, and after a few projects with just a little use, I went full in and started building apps using ReactiveCocoa, leveraging FRP from the ground up.  Reactive Function Programming is one of those things that can be difficult to understand the benefits of.  But I can tell you that once you become used to the new paradigm, it will be difficult to go back.  Especially when developing in a multi-threaded environment.  It spoils you by often eliminating the 'delegate' pattern Apple has advocated for so long, primarily in objective-c.  

Functional Reactive Programming is a big departure from traditional Object Oriented programming.  The learning curve is pretty steep.  A few years ago when I first explored using ReactiveCocoa for my iOS projects, the concept was pretty obscure in the world of development, but the idea is catching on.  Frameworks like React.js and languages like elm are making the idea more mainstream.  ReactiveCocoa is no longer the only option when developing in swift.  There is another popular library emerging called RxSwift gained some steam while the ReactiveCocoa project work to re-develop itself for the world of Swift.  I may try using RxSwift for my next app.  I like it's simplicity and fairly strict adherence to the more general "Rx" standard of programming.

I mentioned earlier that learning the basics, and describing them can be difficult.  For that reason I've started this "series" of posts with a list of resources that in the past have done a really good job of helping me understand the world of Reactive Programming over the years.

Resources

This particular post is fairly disappointing.  I'm not giving any concrete examples, or imparting any wisdom from working in the Reactive Programming world.  Those posts will come.  This is just an introduction.

The ReactiveCocoa Project

There are many implementations of FRP in most of the popular languages.  I believe Java, and .NET were among the first to really get widespread adoption.  In the world of Cocoa programming (Swift/Objective-C) the framework I am most familiar with is ReactiveCocoa.  

A little history on this project.  

Version 2.x was to this point the most popular.  However, it was Obj-C.  It was stable and fantastic, but oh so many square brackets!!!  ReactiveCocoa relies heavily on chaining methods.  For every method chained, a new set of square brackets.  It wasn't uncommon to have a statement, beginning like

[[[[[[RACObserve(self, username)...
    

Yikes.  But you eventually got used to it.  You have to be comfortable with brackets in order to be a regular objective-C programmer.  

With the advent of Swift, a new version of ReactiveCocoa made it to release after a significant overhaul.  Not just porting code, but taking a step back and leveraging the strengths of Swift.  And best of all, no more square brackets!  Statements looked a bit cleaner.  Chaining was accomplished with a "propriatiary" |> operator.  Kinda Neat.  Problem is that when v3.0 finally had an official release. Swift 2.0 was in full swing, and an update was needed.

And this is where we find ourselves now.  The |> has been replaced with a more appropriate period.  And new Swift syntax is being leveraged.  As of this blog post ReactiveCocoa 4.0.1 is available having just become an official release.  This is where I'll pick up and start working through examples.  So for specific syntax and detailed usage I would recommend looking at the links referred to above.  The first thing I want to explore is the architecture of an FRP based app in ReactiveCocoa.

 

Need a fresh perspective on a tough project?

Let’s talk about how RDG can help.

Contact Us