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
90
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
410
the ionic crash course
appfoundrybe
1
170
View based apps with Conductor
appfoundrybe
0
590
Android Accessibility at GDG Devfest Brussels 2016
appfoundrybe
0
470
Other Decks in Programming
See All in Programming
各クラウドサービスにおける.NETの対応と見解
ymd65536
0
230
Amazon S3 NYJavaSIG 2024-12-12
sullis
0
130
Fixstars高速化コンテスト2024準優勝解法
eijirou
0
150
20241217 競争力強化とビジネス価値創出への挑戦:モノタロウのシステムモダナイズ、開発組織の進化と今後の展望
monotaro
PRO
0
200
バグを見つけた?それAppleに直してもらおう!
uetyo
0
200
Beyond ORM
77web
11
1.5k
ChatGPT とつくる PHP で OS 実装
memory1994
PRO
3
160
命名をリントする
chiroruxx
1
530
これでLambdaが不要に?!Step FunctionsのJSONata対応について
iwatatomoya
2
3.9k
선언형 UI에서의 상태관리
l2hyunwoo
0
250
HTML/CSS超絶浅い説明
yuki0329
0
140
なまけものオバケたち -PHP 8.4 に入った新機能の紹介-
tanakahisateru
1
140
Featured
See All Featured
JavaScript: Past, Present, and Future - NDC Porto 2020
reverentgeek
47
5.1k
I Don’t Have Time: Getting Over the Fear to Launch Your Podcast
jcasabona
29
2k
Fontdeck: Realign not Redesign
paulrobertlloyd
82
5.3k
StorybookのUI Testing Handbookを読んだ
zakiyama
28
5.4k
Cheating the UX When There Is Nothing More to Optimize - PixelPioneers
stephaniewalter
280
13k
Sharpening the Axe: The Primacy of Toolmaking
bcantrill
38
1.9k
The Straight Up "How To Draw Better" Workshop
denniskardys
232
140k
Done Done
chrislema
182
16k
Building Your Own Lightsaber
phodgson
104
6.1k
How to Ace a Technical Interview
jacobian
276
23k
Designing on Purpose - Digital PM Summit 2013
jponch
116
7k
Designing Experiences People Love
moore
139
23k
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