ReactiveCocoa
joehsieh
2014/02/13
Outline
•

What is reactiveCocoa?

•

RAC basic components

•

MVVM
What is ReactiveCocoa?

•

ReactiveCocoa = Functional Reactive(Binding)
Programming
MVC
ViewController exists many glue code.
Cocoa Binding(Mac Only)
•

The Cocoa bindings technology provides a
means of keeping model and view values
synchronized wi...
Demo for Cocoa Binding
Binding for iOS?

•

Binding (Reactive) version(Mac Only) is based
on KVO, so we can also achieve binding by KVO
in iOS.
KVO Demo
RAC basic components
RACStream
•

It applies higher-oder functions to handle
incoming data (map, reduce, filter, ect…)
•

for binding values : R...
RACSignal
filter
subject

v

map
v

observer

!

•

value: nextValue, error or complete message.
KVO
[RACObserve(self, username) subscribeNext:^(NSString
*newName) {!
NSLog(@"%@", newName);!
}];
Chained Signal
[[RACObserve(self, username)!
filter:^(NSString *newName) {!
return [newName hasPrefix:@"j"];!
}]!
subscrib...
Derive state

RAC(self, createEnabled) = [RACSignal !
combineLatest:@[ RACObserve(self, password),
RACObserve(self, passwo...
Trigged after a group of
operations completes

[[RACSignal !
merge:@[ [client fetchUserRepos], [client fetchOrgRepos] ]] !...
Solves callback hell
[[[[client !
logInUser] !
flattenMap:^(User *user) {!
// Return a signal that loads cached messages f...
Advantages
•

RAC is that it provides a single, unified
approach to dealing with asynchronous
behaviors, including delegate...
RACSequence

•

Adds higher-order functions to collection.

•

It is pull-driven(works on demand)
Imperative Programming
testData is an array from 1 to 100
NSMutableArray *result = [NSMutableArray
array];
for (NSNumber *...
Functional Programming
RACSequence *sequence = [[[testData rac_sequence]
map:^id(id value) {
return @(pow([value intValue]...
Demo
BMI Imperative
BMI RAC
height text
field

180cm

weight
text field

height

weight

75kg

combine
Input
BMIValue
label

23.15

BMIValue

Ou...
Refines BMI by MVVM

Separates presentation and validation logic from controller
to viewModel.
Benefits of MVVM

•

We can easily Unit Test the View Model

•

It’s easier to change the UI
Reference
•

http://elm-lang.org/learn/What-is-FRP.elm

•

http://blog.maybeapps.com/post/42894317939/input-and-output

•
...
Upcoming SlideShare
Loading in...5
×

Reactive cocoa

835

Published on

0 Comments
10 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
835
On Slideshare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
18
Comments
0
Likes
10
Embeds 0
No embeds

No notes for slide

Reactive cocoa

  1. 1. ReactiveCocoa joehsieh 2014/02/13
  2. 2. Outline • What is reactiveCocoa? • RAC basic components • MVVM
  3. 3. What is ReactiveCocoa? • ReactiveCocoa = Functional Reactive(Binding) Programming
  4. 4. MVC ViewController exists many glue code.
  5. 5. Cocoa Binding(Mac Only) • The Cocoa bindings technology provides a means of keeping model and view values synchronized without you having to write a lot of “glue code.”(less glue code) It allows you to establish a mediated connection between a view and a piece of data, “binding” them such that a change in one is reflected in the other(reactive).
  6. 6. Demo for Cocoa Binding
  7. 7. Binding for iOS? • Binding (Reactive) version(Mac Only) is based on KVO, so we can also achieve binding by KVO in iOS.
  8. 8. KVO Demo
  9. 9. RAC basic components
  10. 10. RACStream • It applies higher-oder functions to handle incoming data (map, reduce, filter, ect…) • for binding values : RACSignal(Push-driven) • for collection : RACSequence(Pull-driven)
  11. 11. RACSignal filter subject v map v observer ! • value: nextValue, error or complete message.
  12. 12. KVO [RACObserve(self, username) subscribeNext:^(NSString *newName) {! NSLog(@"%@", newName);! }];
  13. 13. Chained Signal [[RACObserve(self, username)! filter:^(NSString *newName) {! return [newName hasPrefix:@"j"];! }]! subscribeNext:^(NSString *newName) {! NSLog(@"%@", newName);! }];
  14. 14. Derive state RAC(self, createEnabled) = [RACSignal ! combineLatest:@[ RACObserve(self, password), RACObserve(self, passwordConfirmation) ] ! reduce:^(NSString *password, NSString *passwordConfirm) { return @([passwordConfirm isEqualToString:password]); }];
  15. 15. Trigged after a group of operations completes [[RACSignal ! merge:@[ [client fetchUserRepos], [client fetchOrgRepos] ]] ! subscribeCompleted:^{! NSLog(@"They're both done!");! }];
  16. 16. Solves callback hell [[[[client ! logInUser] ! flattenMap:^(User *user) {! // Return a signal that loads cached messages for the user.! return [client loadCachedMessagesForUser:user];! }]! flattenMap:^(NSArray *messages) {! // Return a signal that fetches any remaining messages.! return [client fetchMessagesAfterMessage:messages.lastObject];! }]! subscribeNext:^(NSArray *newMessages) {! NSLog(@"New messages: %@", newMessages);! } completed:^{! NSLog(@"Fetched all messages.");! }];
  17. 17. Advantages • RAC is that it provides a single, unified approach to dealing with asynchronous behaviors, including delegate methods, callback blocks, target-action mechanisms, notifications, and KVO.
  18. 18. RACSequence • Adds higher-order functions to collection. • It is pull-driven(works on demand)
  19. 19. Imperative Programming testData is an array from 1 to 100 NSMutableArray *result = [NSMutableArray array]; for (NSNumber *i in testData) { NSUInteger tempValue = @(pow([i intValue] , 2)); if (tempValue % 2 == 0) { [result addObject:@(tempValue)]; } }
  20. 20. Functional Programming RACSequence *sequence = [[[testData rac_sequence] map:^id(id value) { return @(pow([value intValue] , 2)); }] filter:^BOOL(id value) { return ([value intValue] % 2 == 0); }]; // evaluate sequence (pull data from sequence) NSLog(@"%@", [sequence array]);
  21. 21. Demo
  22. 22. BMI Imperative
  23. 23. BMI RAC height text field 180cm weight text field height weight 75kg combine Input BMIValue label 23.15 BMIValue Output map BMIStatus label Normal weight green BMIStaus background color
  24. 24. Refines BMI by MVVM Separates presentation and validation logic from controller to viewModel.
  25. 25. Benefits of MVVM • We can easily Unit Test the View Model • It’s easier to change the UI
  26. 26. Reference • http://elm-lang.org/learn/What-is-FRP.elm • http://blog.maybeapps.com/post/42894317939/input-and-output • https://github.com/ReactiveCocoa/ReactiveCocoa • http://www.teehanlax.com/blog/model-view-viewmodel-for-ios/ • https://leanpub.com/iosfrp • http://blog.devtang.com/blog/2014/02/11/reactivecocoaintroduction/ • http://github.com/joehsieh/RACBMIiOS <— here is my demo
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×