ReactiveCocoa
functional reactive programming in iOS
Andrei Popa
A. current challenges in programming
- keeping state, inputs, asynchronous tasks, callback hell
B. how does Functional Reactive Programming solve them
- the functional aspect
- the reactive aspect
C. ReactiveCocoa - mature FRP implementation for iOS
D. Examples
- the input is all the sources of action for your app: button taps,
keyboard events, timer triggers, GPS events, web service
responses, etc.
- the output at any one time is the result of combining all inputs.
- ft (input) = output
- but unlike the classic input / output design, this input and
output happens more than once. It’s not just a single input -
work - output, the cycle continues while the app is open
first problem: state
first problem: state
And so our only tool for dealing with all these different things is state.
ReactiveCocoa - Functional Reactive Programming concepts in iOS
• obviously this is highly error prone
• to make matters worse, this type of code will often produce
bugs that are incredibly hard, if not impossible to identify in any
automated way
• state introduces complexity. And worse, it introduces
complexity that grows more than linearly with the size of our app
first problem: state
nondeterministic vs deterministic
nondeterministic vs deterministic
- most developers have experienced the problems of non-
determinism when a client calls in with a problem, but the
problem is not reproducible even by retracing the exact
same steps
- if the program was deterministic, then retracing those exact
steps would always produce the error, no exception
- for testing and quality assurance purposes, reproducibility
is essential
types of inputs - iOS
asynchronous operations
- network requests
- resource loading
- image processing
- bluetooth operations
- file I/O
What happens when several different asynchronous operations must
be executed in some set order (serialized)?
asynchronous operations
callback hell aka Pyramid of Doom
asynchronous operations
callback hell aka Pyramid of Doom
functional programming = stateless programming
- fundamental operation is the application of functions to
arguments.
- passing functions and blocks as objects
- there is no mutable state in functional programming
- f (input) = output , always produces the same output
given the same input. Always.
- it doesn’t rely / change on data outside the current function
functional programming = stateless programming
If we had machines that had infinite computational
power, what problems would we be able to solve?
- Alonzo Church developed λ - lambda calculus
Computability described via λ-calculus gave rise to Functional Programming
- Alan Turing developed Turing machine
Computability via Turing machines gave rise to Imperative Programming.
Church-Turing thesis: The notion of intuitive computability is
exactly captured by λ-definability or by Turing computability.
who is using functional programming ?
Some of the most complex systems written using Erlang:
- telecommunication and traffic control systems
- Ericsson (highly tolerant and scalable telecommunication switches)
- Facebook (Facebook chat backend)
- T-Mobile (advanced call control services)
- WhatsApp server
- average of 8 billion inbound messages and 12 billion outbound
messages a day
- WhatsApp scales up to more than a million connections on a single
box thanks to Erlang’s amazing scalability
reactive programming
In computing, reactive programming is a programming paradigm
oriented around data flows and the propagation of change.
We don’t care how it happens – just that we can rely on its truthfulness.
functional reactive programming
time-varying values
- lets us define our app in terms of the time-varying values and
ensures changes propagate as needed.
- the result of asynchronous work is really just a time-varying
value that only has a value once the work is done
- a UI element’s value could be seen as a time-varying value
that changes as the user interacts with it
- if my app is running on a mobile device, the device’s GPS
coordinates is a time-varying value
1. model any reaction to any event using signals
- wrap calls in RACSignal (ObjC) / SignalProducer (Swift)
2. manipulate, transform, combine values sent by signals
- map, filter, flattenMap, zip, concat, merge, …
- throttle, delay, repeat, then, take, distinctUntilChanged, …
- deliverOn(someThread), subscribeOn(someOtherThread)
- subscribe
ReactiveCocoa
provides a common interface for all events : signal
ReactiveCocoa
unifies all of Cocoa’s common patterns for asynchrony and event handling
ReactiveCocoa
unifies all of Cocoa’s common patterns for asynchrony and event handling
ReactiveCocoa - Functional Reactive Programming concepts in iOS
Examples
1) Take pictures
burst mode
2) Apply filter
3) Put frame
4) Send to back
for generating
animated GIF
- (RACSignal *) generateCats {
return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber>
subscriber) {
// my long async call ——————————————————————————-
[[MyCameraManager sharedInstance]
takePicturesBurstMode:^(UIImage *image, NSError *error) {
if (error) {
[subscriber sendError:error];
}
else {
[subscriber sendNext:image];
[subscriber sendCompleted];
}
}];
// ——————————————————————————————————————————————
return nil;
}];
}
1) take pictures
- (UIImage *) applyFilter(UIImage *) {
return ;
}
[[signalFactory generateCats]
map:^id(UIImage *cat) {
return applyFilter(cat);
}];
2) apply filter
- (UIImage *) putFrame(UIImage *)image {
return image ;
}
[[[signalFactory generateCats]
map:^id(UIImage *cat) {
return applyFilter(cat);
}]
map:^id(UIImage *cat) {
return putFrame(cat);
}];
3) put frame
[[[[[signalFactory generateCats]
map:^id(UIImage *cat) {
return applyFilter(cat);
}]
map:^id(UIImage *cat) {
return putFrame(cat);
}]
collect] // combine all next events into one NSArray
flattenMap:^RACStream *(NSArray *allCats) {
// [signalFactory sendAsyncToBackend:allCats] is a RACSignal
return [signalFactory sendAsyncToBackend:allCats];
}]; // this will still be a RACSignal
4) Send to back for generating animated GIF
[[[[[[signalFactory generateCats] // take pictures
map:^id(UIImage *cat) {
return applyFilter(cat);
}] // apply filter
map:^id(UIImage *cat) {
return putFrame(cat);
}]] // put frame
collect] // get all the processed images
flattenMap:^RACStream *(NSArray *allCats) {
return [signalFactory sendAsyncToBackend:allCats];
}] // send async call and wait for the result
// signal is started when someone subscribes
subscribeNext:^(UIImage *animatedGIF) {
// success!, display GIF
}
error:^(NSError *error) {
// error
}];
5) see the result
ReactiveCocoa - Functional Reactive Programming concepts in iOS
ReactiveCocoa - Functional Reactive Programming concepts in iOS
ReactiveCocoa - Functional Reactive Programming concepts in iOS
ReactiveCocoa
Bindings in MVVM architecture using RAC Signals
- http://rxmarbles.com/
- http://www.sprynthesis.com/2014/06/15/why-reactivecocoa/
- http://www.sprynthesis.com/2014/12/06/reactivecocoa-mvvm-
introduction/
- http://www.raywenderlich.com/62699/reactivecocoa-tutorial-pt1
- https://developers.soundcloud.com/blog/building-the-new-ios-app-a-
new-paradigm
- https://github.com/popaaaandrei/StarterProject_RACSwift2
The end. Thank you! Q&A

More Related Content

PDF
Models used in iOS programming, with a focus on MVVM
PDF
MV(C, mvvm) in iOS and ReactiveCocoa
PPTX
Training: MVVM Pattern
PPT
Ppt of Basic MVC Structure
PDF
MVC Seminar Presantation
PPTX
Slide Presentation of MVP Pattern Concept
PPTX
Design Pattern - MVC, MVP and MVVM
PPTX
Advanced java lab swing mvc awt
Models used in iOS programming, with a focus on MVVM
MV(C, mvvm) in iOS and ReactiveCocoa
Training: MVVM Pattern
Ppt of Basic MVC Structure
MVC Seminar Presantation
Slide Presentation of MVP Pattern Concept
Design Pattern - MVC, MVP and MVVM
Advanced java lab swing mvc awt

What's hot (20)

PDF
PPTX
MVVM ( Model View ViewModel )
PPTX
Acrhitecture deisign pattern_MVC_MVP_MVVM
PPT
Mvc 130330091359-phpapp01
PDF
Ui design patterns
PDF
Mvc, mvp, mvvm...
PPTX
MVx patterns in iOS (MVC, MVP, MVVM)
PPT
MVC(Model View Controller),Web,Enterprise,Mobile
PPTX
MVVM with WPF
 
PPTX
MVVM Design Pattern NDC2009
PPTX
Model view controller (mvc)
PPTX
Mvc pattern and implementation in java fair
PPTX
MVVM - Model View ViewModel
PDF
MVVM in iOS presentation
PPT
Why MVC?
PPTX
Mvvm basics
PPTX
Model driven architecture
PPTX
Design pattern
ODP
What is MVC?
MVVM ( Model View ViewModel )
Acrhitecture deisign pattern_MVC_MVP_MVVM
Mvc 130330091359-phpapp01
Ui design patterns
Mvc, mvp, mvvm...
MVx patterns in iOS (MVC, MVP, MVVM)
MVC(Model View Controller),Web,Enterprise,Mobile
MVVM with WPF
 
MVVM Design Pattern NDC2009
Model view controller (mvc)
Mvc pattern and implementation in java fair
MVVM - Model View ViewModel
MVVM in iOS presentation
Why MVC?
Mvvm basics
Model driven architecture
Design pattern
What is MVC?
Ad

Viewers also liked (10)

PDF
Stanfy MadCode Meetup #9: Functional Programming 101 with Swift
PDF
ReactiveCocoa and Swift, Better Together
PDF
Reactive cocoa made Simple with Swift
PDF
Learn You a ReactiveCocoa for Great Good
PDF
ReactiveCocoa Goodness - Part I of II
PDF
ReactiveCocoa in Practice
PDF
Introduction to reactive programming & ReactiveCocoa
PDF
in in der 響應式編程
PDF
ReactiveCocoa - TDC 2016
PDF
Stanfy MadCode Meetup #11: Why do you need to switch from Obj-C to Swift, or ...
Stanfy MadCode Meetup #9: Functional Programming 101 with Swift
ReactiveCocoa and Swift, Better Together
Reactive cocoa made Simple with Swift
Learn You a ReactiveCocoa for Great Good
ReactiveCocoa Goodness - Part I of II
ReactiveCocoa in Practice
Introduction to reactive programming & ReactiveCocoa
in in der 響應式編程
ReactiveCocoa - TDC 2016
Stanfy MadCode Meetup #11: Why do you need to switch from Obj-C to Swift, or ...
Ad

Similar to ReactiveCocoa - Functional Reactive Programming concepts in iOS (20)

PDF
Introduction to Functional Reactive Programming
PPTX
Reactive cocoa 101改
PDF
Reactive Cocoa
PDF
Introduction To Functional Reactive Programming Poznan
PDF
Moving towards Reactive Programming
PDF
Reactive cocoa cocoaheadsbe_2014
PDF
(Functional) reactive programming (@pavlobaron)
PDF
Buy ebook Functional Reactive Programming 1st Edition Stephen Blackheath chea...
PDF
Reactive: Programming -> Systems -> Architecture
PDF
Petr Šíma: Principy funkcionálně reaktivního programovaní na mobilních platfo...
PDF
Code europe
PPTX
Reactive cocoa 101
PDF
Functional Reactive Programming (CocoaHeads Bratislava)
PPTX
Solve it Differently with Reactive Programming
PDF
Flow Base Programming with Node-RED and Functional Reactive Programming with ...
PDF
Becoming reactive without overreacting (@pavlobaron)
PPTX
WTF is Reactive Programming
PPTX
Real world functional reactive programming
PDF
Douglas Crockford: Serversideness
Introduction to Functional Reactive Programming
Reactive cocoa 101改
Reactive Cocoa
Introduction To Functional Reactive Programming Poznan
Moving towards Reactive Programming
Reactive cocoa cocoaheadsbe_2014
(Functional) reactive programming (@pavlobaron)
Buy ebook Functional Reactive Programming 1st Edition Stephen Blackheath chea...
Reactive: Programming -> Systems -> Architecture
Petr Šíma: Principy funkcionálně reaktivního programovaní na mobilních platfo...
Code europe
Reactive cocoa 101
Functional Reactive Programming (CocoaHeads Bratislava)
Solve it Differently with Reactive Programming
Flow Base Programming with Node-RED and Functional Reactive Programming with ...
Becoming reactive without overreacting (@pavlobaron)
WTF is Reactive Programming
Real world functional reactive programming
Douglas Crockford: Serversideness

Recently uploaded (20)

PDF
Hindi spoken digit analysis for native and non-native speakers
PDF
How ambidextrous entrepreneurial leaders react to the artificial intelligence...
PPTX
The various Industrial Revolutions .pptx
PDF
Hybrid horned lizard optimization algorithm-aquila optimizer for DC motor
PDF
Enhancing emotion recognition model for a student engagement use case through...
PPT
Module 1.ppt Iot fundamentals and Architecture
PPTX
Configure Apache Mutual Authentication
PDF
NewMind AI Weekly Chronicles – August ’25 Week III
PDF
Zenith AI: Advanced Artificial Intelligence
PDF
Consumable AI The What, Why & How for Small Teams.pdf
PDF
Flame analysis and combustion estimation using large language and vision assi...
PDF
A comparative study of natural language inference in Swahili using monolingua...
PPTX
Benefits of Physical activity for teenagers.pptx
PPT
Geologic Time for studying geology for geologist
PPTX
AI IN MARKETING- PRESENTED BY ANWAR KABIR 1st June 2025.pptx
PDF
Credit Without Borders: AI and Financial Inclusion in Bangladesh
PDF
UiPath Agentic Automation session 1: RPA to Agents
DOCX
search engine optimization ppt fir known well about this
PDF
Convolutional neural network based encoder-decoder for efficient real-time ob...
PDF
OpenACC and Open Hackathons Monthly Highlights July 2025
Hindi spoken digit analysis for native and non-native speakers
How ambidextrous entrepreneurial leaders react to the artificial intelligence...
The various Industrial Revolutions .pptx
Hybrid horned lizard optimization algorithm-aquila optimizer for DC motor
Enhancing emotion recognition model for a student engagement use case through...
Module 1.ppt Iot fundamentals and Architecture
Configure Apache Mutual Authentication
NewMind AI Weekly Chronicles – August ’25 Week III
Zenith AI: Advanced Artificial Intelligence
Consumable AI The What, Why & How for Small Teams.pdf
Flame analysis and combustion estimation using large language and vision assi...
A comparative study of natural language inference in Swahili using monolingua...
Benefits of Physical activity for teenagers.pptx
Geologic Time for studying geology for geologist
AI IN MARKETING- PRESENTED BY ANWAR KABIR 1st June 2025.pptx
Credit Without Borders: AI and Financial Inclusion in Bangladesh
UiPath Agentic Automation session 1: RPA to Agents
search engine optimization ppt fir known well about this
Convolutional neural network based encoder-decoder for efficient real-time ob...
OpenACC and Open Hackathons Monthly Highlights July 2025

ReactiveCocoa - Functional Reactive Programming concepts in iOS

  • 2. A. current challenges in programming - keeping state, inputs, asynchronous tasks, callback hell B. how does Functional Reactive Programming solve them - the functional aspect - the reactive aspect C. ReactiveCocoa - mature FRP implementation for iOS D. Examples
  • 3. - the input is all the sources of action for your app: button taps, keyboard events, timer triggers, GPS events, web service responses, etc. - the output at any one time is the result of combining all inputs. - ft (input) = output - but unlike the classic input / output design, this input and output happens more than once. It’s not just a single input - work - output, the cycle continues while the app is open first problem: state
  • 4. first problem: state And so our only tool for dealing with all these different things is state.
  • 6. • obviously this is highly error prone • to make matters worse, this type of code will often produce bugs that are incredibly hard, if not impossible to identify in any automated way • state introduces complexity. And worse, it introduces complexity that grows more than linearly with the size of our app first problem: state
  • 8. nondeterministic vs deterministic - most developers have experienced the problems of non- determinism when a client calls in with a problem, but the problem is not reproducible even by retracing the exact same steps - if the program was deterministic, then retracing those exact steps would always produce the error, no exception - for testing and quality assurance purposes, reproducibility is essential
  • 10. asynchronous operations - network requests - resource loading - image processing - bluetooth operations - file I/O What happens when several different asynchronous operations must be executed in some set order (serialized)?
  • 13. functional programming = stateless programming - fundamental operation is the application of functions to arguments. - passing functions and blocks as objects - there is no mutable state in functional programming - f (input) = output , always produces the same output given the same input. Always. - it doesn’t rely / change on data outside the current function
  • 14. functional programming = stateless programming
  • 15. If we had machines that had infinite computational power, what problems would we be able to solve? - Alonzo Church developed λ - lambda calculus Computability described via λ-calculus gave rise to Functional Programming - Alan Turing developed Turing machine Computability via Turing machines gave rise to Imperative Programming. Church-Turing thesis: The notion of intuitive computability is exactly captured by λ-definability or by Turing computability.
  • 16. who is using functional programming ? Some of the most complex systems written using Erlang: - telecommunication and traffic control systems - Ericsson (highly tolerant and scalable telecommunication switches) - Facebook (Facebook chat backend) - T-Mobile (advanced call control services) - WhatsApp server - average of 8 billion inbound messages and 12 billion outbound messages a day - WhatsApp scales up to more than a million connections on a single box thanks to Erlang’s amazing scalability
  • 17. reactive programming In computing, reactive programming is a programming paradigm oriented around data flows and the propagation of change. We don’t care how it happens – just that we can rely on its truthfulness.
  • 18. functional reactive programming time-varying values - lets us define our app in terms of the time-varying values and ensures changes propagate as needed. - the result of asynchronous work is really just a time-varying value that only has a value once the work is done - a UI element’s value could be seen as a time-varying value that changes as the user interacts with it - if my app is running on a mobile device, the device’s GPS coordinates is a time-varying value
  • 19. 1. model any reaction to any event using signals - wrap calls in RACSignal (ObjC) / SignalProducer (Swift) 2. manipulate, transform, combine values sent by signals - map, filter, flattenMap, zip, concat, merge, … - throttle, delay, repeat, then, take, distinctUntilChanged, … - deliverOn(someThread), subscribeOn(someOtherThread) - subscribe ReactiveCocoa provides a common interface for all events : signal
  • 20. ReactiveCocoa unifies all of Cocoa’s common patterns for asynchrony and event handling
  • 21. ReactiveCocoa unifies all of Cocoa’s common patterns for asynchrony and event handling
  • 24. 1) Take pictures burst mode 2) Apply filter 3) Put frame 4) Send to back for generating animated GIF
  • 25. - (RACSignal *) generateCats { return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) { // my long async call ——————————————————————————- [[MyCameraManager sharedInstance] takePicturesBurstMode:^(UIImage *image, NSError *error) { if (error) { [subscriber sendError:error]; } else { [subscriber sendNext:image]; [subscriber sendCompleted]; } }]; // —————————————————————————————————————————————— return nil; }]; } 1) take pictures
  • 26. - (UIImage *) applyFilter(UIImage *) { return ; } [[signalFactory generateCats] map:^id(UIImage *cat) { return applyFilter(cat); }]; 2) apply filter
  • 27. - (UIImage *) putFrame(UIImage *)image { return image ; } [[[signalFactory generateCats] map:^id(UIImage *cat) { return applyFilter(cat); }] map:^id(UIImage *cat) { return putFrame(cat); }]; 3) put frame
  • 28. [[[[[signalFactory generateCats] map:^id(UIImage *cat) { return applyFilter(cat); }] map:^id(UIImage *cat) { return putFrame(cat); }] collect] // combine all next events into one NSArray flattenMap:^RACStream *(NSArray *allCats) { // [signalFactory sendAsyncToBackend:allCats] is a RACSignal return [signalFactory sendAsyncToBackend:allCats]; }]; // this will still be a RACSignal 4) Send to back for generating animated GIF
  • 29. [[[[[[signalFactory generateCats] // take pictures map:^id(UIImage *cat) { return applyFilter(cat); }] // apply filter map:^id(UIImage *cat) { return putFrame(cat); }]] // put frame collect] // get all the processed images flattenMap:^RACStream *(NSArray *allCats) { return [signalFactory sendAsyncToBackend:allCats]; }] // send async call and wait for the result // signal is started when someone subscribes subscribeNext:^(UIImage *animatedGIF) { // success!, display GIF } error:^(NSError *error) { // error }]; 5) see the result
  • 33. ReactiveCocoa Bindings in MVVM architecture using RAC Signals
  • 34. - http://rxmarbles.com/ - http://www.sprynthesis.com/2014/06/15/why-reactivecocoa/ - http://www.sprynthesis.com/2014/12/06/reactivecocoa-mvvm- introduction/ - http://www.raywenderlich.com/62699/reactivecocoa-tutorial-pt1 - https://developers.soundcloud.com/blog/building-the-new-ios-app-a- new-paradigm - https://github.com/popaaaandrei/StarterProject_RACSwift2 The end. Thank you! Q&A