Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
ReactiveCocoa
Search
AppFoundry
April 11, 2014
Programming
2
69
ReactiveCocoa
A brief overview of reactive programming and how it is applied to Objective C.
AppFoundry
April 11, 2014
Tweet
Share
More Decks by AppFoundry
See All by AppFoundry
Introductie iOS - Jens
appfoundrybe
0
88
Android In Practice
appfoundrybe
0
130
Android Introduction 3.0 by Siebe
appfoundrybe
0
110
Android in Practice (long)
appfoundrybe
0
200
React Native - cross-platform mobile app development
appfoundrybe
0
160
React Native Storybook
appfoundrybe
0
400
the ionic crash course
appfoundrybe
1
170
View based apps with Conductor
appfoundrybe
0
570
Android Accessibility at GDG Devfest Brussels 2016
appfoundrybe
0
460
Other Decks in Programming
See All in Programming
Generative AI Use Cases JP (略称:GenU)奮闘記
hideg
1
290
광고 소재 심사 과정에 AI를 도입하여 광고 서비스 생산성 향상시키기
kakao
PRO
0
170
Enabling DevOps and Team Topologies Through Architecture: Architecting for Fast Flow
cer
PRO
0
320
AWS Lambdaから始まった Serverlessの「熱」とキャリアパス / It started with AWS Lambda Serverless “fever” and career path
seike460
PRO
1
260
Amazon Qを使ってIaCを触ろう!
maruto
0
400
タクシーアプリ『GO』のリアルタイムデータ分析基盤における機械学習サービスの活用
mot_techtalk
4
1.4k
『ドメイン駆動設計をはじめよう』のモデリングアプローチ
masuda220
PRO
8
540
弊社の「意識チョット低いアーキテクチャ」10選
texmeijin
5
24k
Content Security Policy入門 セキュリティ設定と 違反レポートのはじめ方 / Introduction to Content Security Policy Getting Started with Security Configuration and Violation Reporting
uskey512
1
530
リアーキテクチャxDDD 1年間の取り組みと進化
hsawaji
1
220
Remix on Hono on Cloudflare Workers
yusukebe
1
280
Ethereum_.pdf
nekomatu
0
460
Featured
See All Featured
Save Time (by Creating Custom Rails Generators)
garrettdimon
PRO
27
840
Designing Experiences People Love
moore
138
23k
The Pragmatic Product Professional
lauravandoore
31
6.3k
The Illustrated Children's Guide to Kubernetes
chrisshort
48
48k
What's in a price? How to price your products and services
michaelherold
243
12k
JavaScript: Past, Present, and Future - NDC Porto 2020
reverentgeek
47
5k
Making the Leap to Tech Lead
cromwellryan
133
8.9k
Optimizing for Happiness
mojombo
376
70k
Why Our Code Smells
bkeepers
PRO
334
57k
Documentation Writing (for coders)
carmenintech
65
4.4k
Principles of Awesome APIs and How to Build Them.
keavy
126
17k
The Cost Of JavaScript in 2023
addyosmani
45
6.7k
Transcript
None
None
OVERVIEW ⇁ FUNCTIONAL REACTIVE PROGRAMMING ⇁ REACTIVECOCOA* ⇁ RESPONDER /
CALLHANDLE? ⇁ CODE EXAMPLES
REACTIVECOCOA* TOPICS ⇁ BUILDING BLOCKS ⇁ COLLECTION TRANSFORMATION ⇁ ASYNCHRONOUS
PROGRAMMING ⇁ COMPOSITION & PARALLELIZATION ⇁ DATA BINDING & MVVM
FUNCTIONAL REACTIVE PROGRAMMING First look at
FRP ❝FRP IS A HIGH-LEVEL WAY TO WORK WITH INTERACTIONS.
IT PROVIDES CONTROL FLOW STRUCTURES FOR TIME. IT IS BUILT AROUND THE IDEA OF TIME-VARYING VALUES, CALLED SIGNALS. ❞ — elm-lang.org
٩۹(๏̯͡๏)ゞ? WAIT. WHAT?!
FRP ❝REPRESENT & QUERY ASYNCHRONOUS DATA STREAMS, AND PARAMETERIZE THE
CONCURRENCY IN THE ASYNCHRONOUS DATA STREAMS USING SCHEDULERS.❞ — rx.codeplex.com
FUNCTIONAL REACTIVE INPUT ⤑ OUTPUT AVOID STATE AVOID MUTABLE DATA
NO SIDE-EFFECTS DECLARATIVE
FUNCTIONAL REACTIVE DATA FLOW CHANGE PROPAGATION SIDE-EFFECTS EVENTS NON-BLOCKING COMPOSITION
(-_-)ゞ
None
None
DIFFERENT PARADIGM NOT JUST A LIBRARY
PROCEDURAL (HOW?)
RISE OF FRP
ERIK MEIJER ⇁ FUNCTIONAL PROGRAMMING & COMPILER IMPLEMENTATION ⇁ MICROSOFT
(2000 – 2013) ⇁ REACTIVE EXTENSIONS (RX) FOR C#/.NET
WHAT’S THE DIFFERENCE BETWEEN
NOTHING.
BOTH ARE JUST
DB EXAMPLE [[[[db getArticles] filter:^BOOL(Article* article) { return article.isDraft; }]
map:^Draft*(Article* article) { Draft *draft = [Draft new]; draft.title = article.title; return draft; }] doNext:^(Draft *draft) { // Do something with each draft }];
TOUCH EXAMPLE [[[[view touchSignals] filter:^BOOL(UITouch* touch) { return touch.duration >
100; }] map:^TouchPoint*(UITouch* touch) { TouchPoint* tp = [TouchPoint new]; tp.x = touch.x; tp.y = touch.y; return tp; }] doNext:^(TouchPoint *tp) { // Do something with each tp }];
RX.NET REACTIVECOCOA RXJAVA* RXJS RXCPP RXPY RXRB BACONJS
FRP IN DESIGN PATTERNS 3-in-1
IENUMERATOR<T> ⇁ ENUMERABLE<T> bool MoveNext()
IOBSERVER<T> void OnCompleted() void OnError() void OnNext() OBSERVABLE<T> Disposable subscribe(Observer<T>)
DISPOSABLE void dispose()
SIGNAL IENUMERATOR<T> IOBSERVER<T> ENUMERABLE<T> DISPOSABLE OBSERVABLE<T>
REACTIVECOCOA Building Blocks
STREAM ⇁ ABSTRACT BASE TYPE ⇁ ANY SERIES OF OBJECT
VALUES ⇁ VALUES AVAILABLE NOW OR IN FUTURE RACStream
SIGNAL ⇁ PUSH-DRIVEN STREAM ⇁ DATA TO BE DELIVERED IN
THE FUTURE ⇁ VALUES SENT AS THEY ‘ARRIVE’ RACSignal A B
⇁ SUBSCRIBE TO RECEIVE EVENTS ⇁ SENDS 3 EVENT TYPES
⇁ ‘NEXT’ ⇁ ‘ERROR’ ⇁ ‘COMPLETE’ ⇁ IMMUTABLE A B
COLD ⇁ DEFAULT ⇁ START WORK WITH EACH NEW SUBSCRIPTION
RACSignal HOT ⇁ WORK STARTS ONCE ⇁ FOR ALL SUBSCRIPTIONS RACMultiCastConnection
SIDE-EFFECTS ⇁ WORK THAT HAPPENS AS SIGNALS MOVE THROUGH ⇁
NEXT ⇁ ERROR ⇁ COMPLETE ⇁ UI ⇁ FIRING OTHER SIGNALS ⇁ …
SEQUENCE ⇁ PULL-DRIVEN STREAM ⇁ DATA ALREADY AVAILABLE ⇁ VALUES
REQUESTED LAZILY RACSequence
SUBJECT ⇁ INHERITS FROM SIGNAL ⇁ MUTABLE ⇁ ‘NEXT’, ‘ERROR’,
‘COMPLETE’ EVENTS FROM OUTSIDE ⇁ BRIDGE NON-REACTIVE CODE RACSubject
SUBSCRIPTION ⇁ WAITS FOR EVENTS FROM A SIGNAL ⇁ DISPOSED
WHEN SIGNAL COMPLETES/ERRORS id<RACSubscriber>
DISPOSABLE ⇁ CANCELLATION & RESOURCE CLEANUP ⇁ UNSUBSCRIBES FROM SIGNALS
⇁ IMPLICIT OR EXPLICIT ⇁ RESULT OF A SUBSCRIPTION RACDisposable
SCHEDULER ⇁ (SERIAL) EXECUTION QUEUE FOR SIGNALS ⇁ SIMILAR TO THREADS
⇁ SUPPORTS CANCELLATION ⇁ NO SYNCHRONOUS EXECUTION ⇁ PREVENTS DEADLOCKS RACScheduler
⇁ CREATES & SUBSCRIBES TO A SIGNAL ⇁ IN RESPONSE
TO UI ACTION ⇁ TYPICAL USES ⇁ HANDLERS ⇁ VIEWMODELS COMMAND RACCommand
BUTTON TAP SVC CALL FILTER MAP ARRAY à UI FLATTENMAP
FILTER
SIGNAL OPERATIONS ⇁ -CONCAT: ⇁ -MERGE: ⇁ -FLATTENMAP: ⇁ -THEN:
SIGNAL OPERATIONS ⇁ -RETRY: ⇁ -IF:THEN:ELSE ⇁ -DELAY: ⇁ -THROTTLE: ⇁ -TAKE: ⇁ -TRY:CATCH: ⇁ +RETURN: operators
COLLECTION TRANSFORMATION
COLLECTION TRANSFORMATION ⇁ HIGHER ORDER FUNCTIONS ⇁ MAP ⇁ FILTER
⇁ FOLD ⇁ REDUCE
EXAMPLE NSArray *got = @[@”Tyrion”, @”Jaime”, @”Daenerys”, @”Jon”]; NSMutableArray *gotPeople
= [[NSMutableArray alloc] init]; for(NSString *name in got) { if([name hasPrefix:@”J”]) { continue; } Person *p = [Person new]; p.name = name; [gotPeople addObject:p] }
EXAMPLE NSArray *got = @[@”Tyrion”, @”Jaime”, @”Daenerys”, @”Jon”]; [[got.rac_sequence filter:^BOOL(NSString
*name) { return [name hasPrefix:@”J”]; }] map:^Person*(NSString *name) { Person *p = [Person new]; p.name = name; return p; }];
ASYNCHRONOUS PROGRAMMING
MODELS ⇁ EVENT-DRIVEN / CALLBACK-BASED ⇁ INTERRUPTS FLOW ⇁ NESTING
= PROBLEMATIC ⇁ FUTURES / PROMISES ⇁ ENCAPSULATES SUCCESS / FAILURE HANDLERS ⇁ SOME SEQUENCING & PARALLELISM ⇁ BOTH: REPRESENT SINGLE VALUES
SIGNALS ⇁ LINEAR FLOW (LIMITED NESTING) ⇁ ENCAPSULATES STREAM OF
VALUES OVER TIME ⇁ INFINITELY COMPOSABLE ⇁ SIMPLIFIED SEQUENCING & PARALLELIZATION ⇁ SERIAL QUEUE
EXAMPLE [[[[[self.service getUpcomingMeetings] collect] doNext:^(NSArray *upcomingMeetings) { self.upcomingMeetings = upcomingMeetings;
}] doError:^(NSError *error) { [[AlertView alertWithError:error] show]; }] subscribeCompleted:^{ [self.tableView reloadData]; }];
EXAMPLE [[[[[self.service getUpcomingMeetings] collect] doNext:^(NSArray *upcomingMeetings) { self.upcomingMeetings = upcomingMeetings;
}] doError:^(NSError *error) { [[AlertView alertWithError:error] show]; }] subscribeCompleted:^{ [self.tableView reloadData]; }]; SIGNAL SUBSCRIPTION SIDE-‐EFFECT SIDE-‐EFFECT OPERATION (return) DISPOSABLE
COMPOSITION & PARALLELIZATION
COMPOSITION ⇁ DEPENDENT TASKS ⇁ E.G. NETWORK REQUESTS ⇁ SIGNAL
CHAINING ⇁ NESTING
COMPOSITION – EXAMPLE [client logInWithSuccess:^{ [client loadCachedMessagesWithSuccess:^(NSArray *messages) { [client
fetchMessagesAfterMessage:messages.lastObject success:^(NSArray *nextMessages) { NSLog(@"Fetched all messages."); } failure:^(NSError *error) { [self presentError:error]; }]; } failure:^(NSError *error) { [self presentError:error]; }]; } failure:^(NSError *error) { [self presentError:error]; }];
COMPOSITION – EXAMPLE [[[[client logIn] then:^{ return [client loadCachedMessages]; }]
flattenMap:^(NSArray *messages) { return [client fetchMessages:messages.lastObject]; }] subscribeError:^(NSError *error) { [self presentError:error]; } completed:^{ NSLog(@"Fetched all messages."); }];
PARALLELIZATION ⇁ ONE-BY-ONE ⇁ [RACSignal -concat:@[signals ]] ⇁ IN PARALLEL ⇁ [RACSignal
-combineLatest:@[signals ]] ⇁ RETURN TYPES? JUST SIGNALS.
DATA BINDING & MVVM ReactiveCocoa
DATA BINDING ⇁ NON-STANDARD ON IOS ⇁ REPLACEMENT FOR KEY-VALUE
OBSERVING (KVO) ⇁ TURNS REGULAR PROPERTIES INTO SIGNALS
DATA BINDING [[RACObserve(self, username) filter:^(NSString *newName) { return [newName hasPrefix:@"j"];
}] subscribeNext:^(NSString *newName) { NSLog(@"%@", newName); }];
MVVM VIEW VIEWMODEL MODEL NOTIFIES COMMANDS REFERENCES
RESPONDER / CALLHANDLE ?
CODE EXAMPLES