SlideShare a Scribd company logo
(Func&onal)+Reac&ve+Programming 
with%Reac*veCocoa 
Florent(Pillet 
fpillet@gmail.com 
@fpillet
Reac%ve!programming?
Reac%ve'programming'is'essen%ally 
working'with 
asynchronous*data*streams
Data!changes!over!$me!and!flows!in!streams! 
processed!with!an!asynchronous,!operator5 
based!logic.
They%are%all%separate,%yet%usually%found%at%the% 
same%place%in%the%code.
Build&modern,&highly&interac3ve,&dynamic& 
applica3ons 
with%minimal%pain.
Learning(curve(is(steep1 
Need$to$let$go$of$impera/ve$habits,$learn$new$abstrac/ons$and$ 
pa8erns 
but... 
1"If"you"read"only"one,"it"should"be"The"introduc6on"to"reac6ve"programming"you've"been"missing
Pays%off%big%+me 
Modular 
Reusable 
Expressive 
Testable(code
Reac%ve'programming'paves'the'way'to' 
elimina'ng)state)and)mutability'from'your' 
code.2 
2"Suggested"reading:"Enemy"of"the"state"presenta6on"by"Jus6n"Sparh9Summmers.
So#what#does#this#reac.ve#thing#look#like?
Signals
Signal of current GPS position (CLLocation) 
@100 @450 @300 @140 
time 
Signal of numerical values (NSNumber) 
@270 
Signal of boolean values (NSNumber) 
@YES @NO @YES
Signal of current GPS position (CLLocation) 
@100 @450 @300 @140 
time 
Signal of numerical values (NSNumber) 
@270 
Signal of boolean values (NSNumber) 
@YES @NO @YES 
Signals(send(events(over( 
&me 
A"signal"is"a"push,driven"stream. 
It#produces#three#types#of#events: 
• next"values 
• an"error 
• a"comple+on
next!values 
A"stream"of"values"delivered"over"0me. 
Signals(produce(zero,(one(or(many(next(values(before(either( 
comple*ng(or(producing(an(error. 
time 
Signal of current GPS position (CLLocation) 
next next next next next next
signal'comple'on 
time 
Signal with result of network request 
next completed
signal'error 
time 
Signal of network request emits error on request failure 
error 
(emits a NSError)
Opera&ons 
• A#signal#can#have#mul0ple#subscribers 
• Opera0ons#subcribe#to#a#signal#and#return#a#new#signal 
• Easy#mul0;staged#transforma0ons 
• Async,#async,#async!
map 
time 
Signal of current GPS position (CLLocation) 
B"="A.map(fn) 
@100 @270 
@450 @300 @140 
A 
B 
Signal of numerical values (NSNumber)
filter 
Signal of numerical values (NSNumber) 
@100 @270 
@450 @300 @140 
B"="A.filter(fn) 
@100 @140 
time 
A 
B
concat 
time 
A 
B 
C 
C"="A.concat(B)
merge 
time 
A 
B 
C 
C"="merge(A,B)
combineLatest 
time 
A 1 3 
B 
C 
6 7 8 
2 
C"="combineLatest(A,B) 
2 
6 
3 
6 
3 
7 
3 
8 
Signal of tuples
switchToLatest 
time 
D A C B 
E 
E"="D.switchToLatest() 
A 
B 
C
Other&opera*ons 
• flatten flattenMap zip zipWith startWith 
• delay throttle sample timeout 
• take takeUntil skip ignoreValues 
• try catch then switch if and or not 
• replay replayLast repeat retry 
..."and"a"lot"more"...
Reac%veCocoa
Main%classes%in%Reac+veCocoa 
• Signals:*RACSignal 
• Tuples:*RACTuple 
• Disposables:*RACDisposable 
• Schedulers:*RACScheduler 
• Commands:*RACCommand*(not*covered*here)
Subscribing*to*signals 
[someSignal subscribeNext:^(id nextValue) { 
// this is called asynchronously, every time a new value 
// is available on the signal 
NSLog(@"Signal emitted new value= %@", nextValue); 
}];
Subscribing*to*signals 
[someSignal subscribeNext:^(id nextValue) { 
NSLog(@"Signal emitted new value= %@", nextValue); 
} error:^(NSError *error) { 
NSLog(@"Signal emitted error %@", error); 
} completed:^{ 
NSLog(@"Signal completed"); 
}];
Unsubscribing*from*signals 
// subscribe 
RACDisposable *disposable = 
[someSignal subscribeNext:^(id nextValue) { 
NSLog(@"Signal emitted new value= %@", nextValue); 
}]; 
// can cancel subscription at any time 
[disposable dispose];
Crea%ng(signals 
• Using'KVO 
• Transforming'exis3ng'signals'into'a'new'signal 
• Dynamically'with'a'generator'block 
• Li>ing'from'the'impera3ve'world 
• Manually'producing'events
Signals(from(variables((KVO) 
Use$the$RACObserve(object,path)$macro$to$update$our$ 
unread$count$label3: 
[RACObserve(self.model, unreadCount) subscribeNext:^(NSNumber *value) { 
self.unreadCountLabel.text = [value stringValue]; 
}]; 
3"Real"developers"use"NSNumberForma+er
Transforming+exis.ng+signals 
- (RACSignal *)colorForUnreadCount { 
return [RACObserve(self.model,unreadCount) // this is a signal 
// 'map' subscribes to the signal above and returns 
// a new signal that sends a color for each new unreadCount 
map:^id(NSNumber *unread) { 
NSInteger count = unread.integerValue; 
return count < 10 ? [UIColor blackColor] : 
count < 20 ? [UIColor orangeColor] : 
[UIColor redColor]; 
}]; 
}
Transforming+exis.ng+signals 
// using the signal created in the previous slide 
[[model colorForUnreadCount] subscribeNext:^(UIColor *color) { 
self.unreadLabel.textColor = color; 
}]; 
// a shorter way to write this (for simple binding cases) 
// see <ReactiveCocoa/RACSusbscriptingAssignmentTrampoline.h> 
RAC(self.unreadLabel, textColor) = model.colorForUnreadCount;
Dynamic(signals 
- (RACSignal *)post:(NSDictionary *)formData toURL:(NSURL *)url { 
return [RACSignal createSignal:^(id<RACSubscriber> subscriber)] { 
// use AFNetworking to post form data 
NSURLSessionDataTask *task = [self.sessionManager POST:url parameters:data 
success:^(NSURLSessionDataTask *t, NSDictionary *responseObject) { 
if (responseObject) 
[subscriber sendNext:responseObject]; 
[subscriber sendCompleted]; 
} 
failure:^(NSURLSessionDataTask *t, NSError *error) { 
[subscriber sendError:error]; 
} 
]; 
return [RACDisposable disposableWithBlock:^{ 
[task cancel]; 
}]; 
}]; 
}
Dynamic(signals 
// use signal defined in previous slide 
RACSignal *postSignal = [manager post:@{@"name": @"Florent"} toURL:someURL]; 
[postSignal subscribeNext:^(NSDictionary *response) { 
NSLog(@"Server answered POST with %@", response); 
} error:^(NSError *error) { 
NSLog(@"POST failed with error: %@", error); 
} completed:{ 
NSLog(@"POST was successful"); 
}]
Li#ing&to&the&reac.ve&world 
[[[[[self 
rac_signalForSelector:@selector(locationManager:didRangeBeacons:inRegion:) 
fromProtocol:@protocol(CLLocationManagerDelegate)] 
reduceEach:^(CLLocationManager *manager, NSArray *beacons, CLBeaconRegion *region) { 
return [[beacons sortedArrayUsingFunction:proximityComparator context:NULL] 
firstObject] ?: [NSNull null]; 
}] 
filter:^BOOL(id value) { 
return [value isKindOfClass:[CLBeacon class]]; 
}] 
distinctUntilChanged] 
subscribeNext:^(CLBeacon *beacon) { 
NSLog(@"Last closest beacon: %@.%@", beacon.major, beacon.minor); 
}];
Manual&signals 
@property (strong) RACSubject *manualSignal; 
- (id)init { 
if (self = [super init]) { 
self.manualSignal = [[RACSubject alloc] init]; 
} 
return self; 
} 
- (void)dealloc { 
[self.manualSignal sendCompleted]; 
} 
- (RACSignal *)dataSignal { 
return self.manualSignal; 
} 
- (void)newDataObtained:(id)data { 
[self.manualSignal sendNext:data]; 
}
Manual&signals 
Note%that: 
• **RACSubject**#doesn't#automa.cally#emit#a#completed# 
event#on#dealloc.#You#must#do#it#manually. 
• Use#**RACReplaySubject**#to#create#a#subject#that#can# 
resend#one#or#more#of#the#last#next#values#to#new#subscribers. 
• Avoid#using#subjects#if#you#have#alterna.ves.
Disposables 
Any$subscrip,on$returns$a$RACDiposable.$Use$it$to$cancel$the$ 
subscrip,on
Schedulers 
• Based'on'serial'queues 
• Makes'cancella&on'easy! 
• Use'for'5mers'and'to'control'delivery'of'signals 
RACSignal *onMainThread = 
[signal deliverOn:[RACScheduler mainThreadScheduler]]; 
RACSignal *onSomeSerialQueue = 
[signal deliverOn:[[RACTargetQueueScheduler alloc] 
initWithName:@"My queue scheduler" 
targetQueue:someSerialQueue]]
Schedulers 
// A one-time timer that fires after 1 second on main thread 
RACDisposable *timer = [[RACScheduler mainThreadScheduler] 
afterDelay:1.0 
schedule:^{ 
NSLog(@"Delayed logging"); 
}]; 
// We can cancel this at any time 
[timer dispose];
Schedulers 
// A cancellable periodic action 
RACDisposable *timer = [[RACScheduler schedulerWithPriority:RACSchedulerPriorityDefault] 
after:[NSDate dateWithTimeIntervalSinceNow:1.0] 
repeatingEvery:0.5 
withLeeway:0.1 
schedule:^{ 
NSLog(@"Running periodic action on private queue"); 
}]; 
// Later: stop repeating 
[timer dispose];
Other&Reac*veCocoa&gems 
• @weakify @strongify @unsafeify 
• @onExit 
#import <ReactiveCocoa/RACExtScope.h> 
@weakify(self); 
[signal subscribeNext:^(id value) { 
@strongify(self); 
[self doSomethingWith:value]; 
}];
..."and"there"is"a"lot"more"... 
Commands,)sequences,)signal)mul1cas1ng,)side)effects,)channels,) 
backtraces)&)debugging)features,)event)materializa1on)and) 
dematerializa1on,)tes1ng)are)among)topics)not)covered)here. 
Framework)source)code)and)the)docset)for)Dash)are)useful) 
resources.
More%usage%examples 
- (RACSignal *)numberOfUnreadItems 
{ 
@weakify(self); 
return [[[[[self 
itemsUpdated] 
startWith:@YES] 
map:^(id updated) { 
@strongify(self); 
return self.unreadItemsCount; 
}] 
distinctUntilChanged] 
deliverOn:RACScheduler.mainThreadScheduler]; 
}]; 
}
More%usage%examples 
// Automatically update a badge on tab bar 
// when the count of unread items changes 
- (void)keepNewsItemUpToDate:(UITabBarItem *)newsItem { 
@weakify(newsItem); 
[self.model.numberOfUnreadItems subscribeNext:^(NSNumber *count) { 
@strongify(newsItem); 
if (count.integerValue) 
newsItem.badge = count.stringValue; 
else 
newsItem.badge = @""; 
}]; 
}
Links 
• Reac&veCocoa*framework 
• A*demo*project:*Reac&veWeather 
• The*introduc&on*to*reac&ve*programming*you've*been*missing 
• Enemy*of*the*state 
• Reac&ve*MVVM*(ModelFViewFViewModel):*perfect*match 
Also%look%up%'Reac.ve'%on%Github%and%filter%by%langage%(Obj>C).
Scary&interlude 
Yellow&parts&are&my&app. 
Not$always$like$this. 
This$shouldn't$put$you$off!
Summary
Reac%ve'programming'is'a'logical'way'to' 
model'and'react'to 
asynchronous'informa%on'flow
Reac%ve'code'clearly5'exposes'logic'and' 
transforma-ons'deriving'from'new'data 
5"finding"the"syntax"weird?"remember"your"first"steps"with"Objec:ve<C"and"all"these"square"brackets...
Enforcing)the)separa0on)between)data) 
producers)and)consumers,)reac0ve)code)is) 
more%testable
Once%trained%to%think%reac.vely, 
reducing)state)and)mutability 
is#the#next#logical#step#towards#code#safety,# 
stability#and#predictability.
Reac%veCocoa)is)a)rich)and)powerful)reac%ve) 
programming)framework)that)will)bring)your) 
code)to)a)new)level
Reac%veCocoa)is)a)rich)and)powerful)reac%ve) 
programming)framework)that)will)bring)your) 
code)to)a)new)level 
works&with&Swi+,&too4 
4"See"Colin"Eberhardt's"posts"for"Swi6,"Reac:veCocoa"and"MVVM"pr0n.
Thank&You&! 
Q"&"A 
fpillet@gmail.com 
@fpillet

More Related Content

PDF
Swift Sequences & Collections
PDF
Talk KVO with rac by Philippe Converset
PDF
Realm.io par Clement Sauvage
PDF
Map kit light
PPTX
Avoiding Callback Hell with Async.js
PDF
ReactiveCocoa Goodness - Part I of II
PDF
Callbacks and control flow in Node js
PDF
ReactiveCocoa in Practice
Swift Sequences & Collections
Talk KVO with rac by Philippe Converset
Realm.io par Clement Sauvage
Map kit light
Avoiding Callback Hell with Async.js
ReactiveCocoa Goodness - Part I of II
Callbacks and control flow in Node js
ReactiveCocoa in Practice

What's hot (20)

PPTX
Avoiding callback hell in Node js using promises
PDF
Node Boot Camp
PDF
An Introduction to Reactive Cocoa
PDF
Javascript Promises/Q Library
PDF
Understanding Asynchronous JavaScript
PDF
Asynchronous programming done right - Node.js
PDF
Callbacks, promises, generators - asynchronous javascript
PPTX
All you need to know about the JavaScript event loop
PDF
Découvrir dtrace en ligne de commande.
PDF
ES6: The Awesome Parts
PDF
Reactive cocoa made Simple with Swift
PDF
ReactiveCocoa and Swift, Better Together
PDF
IoT Best practices
PDF
How to send gzipped requests with boto3
PDF
Fast C++ Web Servers
PDF
Présentation de HomeKit
PDF
What's New in ES6 for Web Devs
KEY
Node.js - Best practices
PDF
Reactive Programming Patterns with RxSwift
PPTX
Introduction to es6
Avoiding callback hell in Node js using promises
Node Boot Camp
An Introduction to Reactive Cocoa
Javascript Promises/Q Library
Understanding Asynchronous JavaScript
Asynchronous programming done right - Node.js
Callbacks, promises, generators - asynchronous javascript
All you need to know about the JavaScript event loop
Découvrir dtrace en ligne de commande.
ES6: The Awesome Parts
Reactive cocoa made Simple with Swift
ReactiveCocoa and Swift, Better Together
IoT Best practices
How to send gzipped requests with boto3
Fast C++ Web Servers
Présentation de HomeKit
What's New in ES6 for Web Devs
Node.js - Best practices
Reactive Programming Patterns with RxSwift
Introduction to es6
Ad

Viewers also liked (20)

KEY
NSLogger - Cocoaheads Paris Presentation - English
PDF
Thinking Reactive
PDF
MVVM & RxSwift
PPT
Yhdessä oppimista verkossa
PPT
Juliana New York
PPT
Lunch Recipe from Romania
PDF
One Source Solutions
PDF
Formative Capitalism
PPTX
Facebook
PDF
10 Forecasts Bangladesh Telco Industry
PPTX
Twelve Gods of Olympus
PPTX
NCrafts.IO 2015 - Future of User eXperiences
PPT
Polish Cuisine Book
PPTX
Welcome to Dywity
PPT
eTwinning - lyhyt johdatus työpajatyöskentelyyn
PDF
Iside 26 05e Frodi Assicurazioni
ODP
Exercici11.3
PPT
Oriveden Keskuskoulu - Christmas Greeting from Preschool
PDF
Ibiza Charter Boat: Sunseeker Predator 68 | THE DOER IBIZA. Bookings: + 34 63...
PPTX
Using Moodle to Support Differentiated Instruction
NSLogger - Cocoaheads Paris Presentation - English
Thinking Reactive
MVVM & RxSwift
Yhdessä oppimista verkossa
Juliana New York
Lunch Recipe from Romania
One Source Solutions
Formative Capitalism
Facebook
10 Forecasts Bangladesh Telco Industry
Twelve Gods of Olympus
NCrafts.IO 2015 - Future of User eXperiences
Polish Cuisine Book
Welcome to Dywity
eTwinning - lyhyt johdatus työpajatyöskentelyyn
Iside 26 05e Frodi Assicurazioni
Exercici11.3
Oriveden Keskuskoulu - Christmas Greeting from Preschool
Ibiza Charter Boat: Sunseeker Predator 68 | THE DOER IBIZA. Bookings: + 34 63...
Using Moodle to Support Differentiated Instruction
Ad

Similar to Introduction to reactive programming & ReactiveCocoa (20)

PDF
Router Queue Simulation in C++ in MMNN and MM1 conditions
PPT
Cpp tutorial
PPT
CppTutorial.ppt
PPTX
Lambdas puzzler - Peter Lawrey
PDF
Learn You a ReactiveCocoa for Great Good
PDF
Reactive cocoa
PDF
Intro to ReactiveCocoa
PDF
Intro to React
PDF
Profiling Ruby
ZIP
PDF
MLflow with R
PPTX
Don't Be Afraid of Abstract Syntax Trees
PDF
Emerging Languages: A Tour of the Horizon
PDF
Complex Made Simple: Sleep Better with TorqueBox
PDF
The Evolution of Async-Programming (SD 2.0, JavaScript)
PDF
React Development with the MERN Stack
PDF
Background Jobs - Com BackgrounDRb
PPT
Server side JavaScript: going all the way
KEY
Wider than rails
PDF
The Evolution of Async-Programming on .NET Platform (.Net China, C#)
Router Queue Simulation in C++ in MMNN and MM1 conditions
Cpp tutorial
CppTutorial.ppt
Lambdas puzzler - Peter Lawrey
Learn You a ReactiveCocoa for Great Good
Reactive cocoa
Intro to ReactiveCocoa
Intro to React
Profiling Ruby
MLflow with R
Don't Be Afraid of Abstract Syntax Trees
Emerging Languages: A Tour of the Horizon
Complex Made Simple: Sleep Better with TorqueBox
The Evolution of Async-Programming (SD 2.0, JavaScript)
React Development with the MERN Stack
Background Jobs - Com BackgrounDRb
Server side JavaScript: going all the way
Wider than rails
The Evolution of Async-Programming on .NET Platform (.Net China, C#)

Recently uploaded (20)

PPTX
Odoo POS Development Services by CandidRoot Solutions
PPTX
Online Work Permit System for Fast Permit Processing
PDF
How to Confidently Manage Project Budgets
PDF
PTS Company Brochure 2025 (1).pdf.......
PPTX
What to Capture When It Breaks: 16 Artifacts That Reveal Root Causes
PPTX
FLIGHT TICKET RESERVATION SYSTEM | FLIGHT BOOKING ENGINE API
PPTX
VVF-Customer-Presentation2025-Ver1.9.pptx
PDF
Best Practices for Rolling Out Competency Management Software.pdf
PDF
top salesforce developer skills in 2025.pdf
PPTX
Mastering-Cybersecurity-The-Crucial-Role-of-Antivirus-Support-Services.pptx
PDF
Claude Code: Everyone is a 10x Developer - A Comprehensive AI-Powered CLI Tool
PPTX
Materi-Enum-and-Record-Data-Type (1).pptx
PPTX
Presentation of Computer CLASS 2 .pptx
PDF
How to Migrate SBCGlobal Email to Yahoo Easily
PDF
Digital Strategies for Manufacturing Companies
DOCX
The Five Best AI Cover Tools in 2025.docx
PPTX
ISO 45001 Occupational Health and Safety Management System
PPTX
Materi_Pemrograman_Komputer-Looping.pptx
PDF
The Role of Automation and AI in EHS Management for Data Centers.pdf
PDF
medical staffing services at VALiNTRY
Odoo POS Development Services by CandidRoot Solutions
Online Work Permit System for Fast Permit Processing
How to Confidently Manage Project Budgets
PTS Company Brochure 2025 (1).pdf.......
What to Capture When It Breaks: 16 Artifacts That Reveal Root Causes
FLIGHT TICKET RESERVATION SYSTEM | FLIGHT BOOKING ENGINE API
VVF-Customer-Presentation2025-Ver1.9.pptx
Best Practices for Rolling Out Competency Management Software.pdf
top salesforce developer skills in 2025.pdf
Mastering-Cybersecurity-The-Crucial-Role-of-Antivirus-Support-Services.pptx
Claude Code: Everyone is a 10x Developer - A Comprehensive AI-Powered CLI Tool
Materi-Enum-and-Record-Data-Type (1).pptx
Presentation of Computer CLASS 2 .pptx
How to Migrate SBCGlobal Email to Yahoo Easily
Digital Strategies for Manufacturing Companies
The Five Best AI Cover Tools in 2025.docx
ISO 45001 Occupational Health and Safety Management System
Materi_Pemrograman_Komputer-Looping.pptx
The Role of Automation and AI in EHS Management for Data Centers.pdf
medical staffing services at VALiNTRY

Introduction to reactive programming & ReactiveCocoa