An Introduction to
Reactive Cocoa
Ara Hacopian
BohConf
July 2013
Ara Hacopian
www.smartlogic.io
twitter.com/ahacop
github.com/ahacop
SmartLogic
What is Reactive Cocoa?
RAC is an Objective-C framework for
Functional Reactive Programming
Reactive Programming?
A programming paradigm oriented around
data flows and propagation of change.
Imperative
int x, y = 1, z = 1;
x = y + z; // x = 2; y = 1; z = 1;
y = 3; // x = 2; y = 3; z = 1;
NSLog(@"%d", x); // "2"
Reactive
int x, y = 1, z = 1;
x = y + z; // x = 2; y = 1; z = 1;
y = 3; // x = 4; y = 3; z = 1;
NSLog(@"%d", x); // "4"
We have this: KVO
// In your viewDidLoad/awakeFromNib/init
[self addObserver:self
forKeyPath: @"someString"
options:NSKeyV...
Do it in RAC
[RACAble(self.someString) distinctUntilChanged]
subscribeNext:^(NSString *string) {
// Do a bunch of things h...
Streams
A series of object values.
● Accessed sequentially.
● Available immediately or in the future.
● Allow for declarat...
Sequence
A pull-driven stream.
● Lazily evaluated collections
Sequence Example
// words = @[ NSArray containing words ]
RACSequence *normalizedLongWords =
[[words.rac_sequence
filter:^...
Signal
A push-driven stream.
● Generally represents data that will be
delivered in the future.
● Values are sent on the si...
Signal Example
[self.textField.rac_textSignal
subscribeNext:^(NSString *value) {
NSLog(@"Text field updated: %@",
value);
...
Signal Example 2
[[self.textField.rac_textSignal
filter:^BOOL(NSString *value) {
return [value length] >= 3;
}]
subscribeN...
RACSignal *formValid = [RACSignal
combineLatest:@[
self.userNameField.rac_textSignal,
self.emailField.rac_textSignal,
self...
RACCommand
self.loginCommand = [RACCommand command];
self.loginSignals = [self.loginCommand
addSignalBlock:^(id sender) {
...
Resources
https://github.com/ReactiveCocoa/ReactiveCocoa
https://pinboard.in/u:andrewsardone/t:ReactiveCocoa
Questions?
http://www.smartlogic.io
http://www.twitter.com/smartlogic
http://www.github.com/smartlogic
http://www.facebook...
An Introduction to Reactive Cocoa
Upcoming SlideShare
Loading in …5
×

An Introduction to Reactive Cocoa

4,589
-1

Published on

An intro to Reactive Cocoa from SmartLogic's Ara Hacopian.

Published in: Technology, Education
0 Comments
5 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
4,589
On Slideshare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
43
Comments
0
Likes
5
Embeds 0
No embeds

No notes for slide

An Introduction to Reactive Cocoa

  1. 1. An Introduction to Reactive Cocoa Ara Hacopian BohConf July 2013
  2. 2. Ara Hacopian www.smartlogic.io twitter.com/ahacop github.com/ahacop SmartLogic
  3. 3. What is Reactive Cocoa? RAC is an Objective-C framework for Functional Reactive Programming
  4. 4. Reactive Programming? A programming paradigm oriented around data flows and propagation of change.
  5. 5. Imperative int x, y = 1, z = 1; x = y + z; // x = 2; y = 1; z = 1; y = 3; // x = 2; y = 3; z = 1; NSLog(@"%d", x); // "2"
  6. 6. Reactive int x, y = 1, z = 1; x = y + z; // x = 2; y = 1; z = 1; y = 3; // x = 4; y = 3; z = 1; NSLog(@"%d", x); // "4"
  7. 7. We have this: KVO // In your viewDidLoad/awakeFromNib/init [self addObserver:self forKeyPath: @"someString" options:NSKeyValueObservingOptionNew context:&someStringChangeContext]; // In dealloc [self removeObserver: self forKeyPath: @"someString" context:&someStringChangeContext]; // Elsewhere in your class - (void)observeValueForKeyPath:( NSString *)keyPath ofObject:( id)object change:( NSDictionary *)change context:( void *)context { if (context == &someStringChangeContext) { if ([keyPath isEqualToString: @"someString"]) { // Do a bunch of stuff here } } }
  8. 8. Do it in RAC [RACAble(self.someString) distinctUntilChanged] subscribeNext:^(NSString *string) { // Do a bunch of things here, just like you would with KVO }]; Source: http://tonyarnold.me/post/reactivelessons
  9. 9. Streams A series of object values. ● Accessed sequentially. ● Available immediately or in the future. ● Allow for declarative transforms Two types: 1. Signals 2. Sequences
  10. 10. Sequence A pull-driven stream. ● Lazily evaluated collections
  11. 11. Sequence Example // words = @[ NSArray containing words ] RACSequence *normalizedLongWords = [[words.rac_sequence filter:^ BOOL (NSString *word) { return [word length] >= 10; }] map:^(NSString *word) { return [word lowercaseString]; }];
  12. 12. Signal A push-driven stream. ● Generally represents data that will be delivered in the future. ● Values are sent on the signal ● Users subscribe to access values 3 types of events sent to subscribers: 1. Next - provides next value 2. Error 3. Completed
  13. 13. Signal Example [self.textField.rac_textSignal subscribeNext:^(NSString *value) { NSLog(@"Text field updated: %@", value); }];
  14. 14. Signal Example 2 [[self.textField.rac_textSignal filter:^BOOL(NSString *value) { return [value length] >= 3; }] subscribeNext:^(NSString *value) { NSLog(@"Text field updated: %@", value); }];
  15. 15. RACSignal *formValid = [RACSignal combineLatest:@[ self.userNameField.rac_textSignal, self.emailField.rac_textSignal, self.confirmEmailField.rac_textSignal, ] reduce:^(NSString *userName, NSString *email, NSString *confirmEmail) { return @(username.length > 0 && email.length > 0 && confirmEmail.length > 0 && [email isEqualToString:confirmEmail]); }]; RAC(self.createAccountBtn.enabled) = formValid;
  16. 16. RACCommand self.loginCommand = [RACCommand command]; self.loginSignals = [self.loginCommand addSignalBlock:^(id sender) { return [client logIn]; }]; [self.loginSignals subscribeNext:^(RACSignal *loginSignal) { [loginSignal subscribeCompleted:^(id _) { NSLog(@"Logged in successfully!"); }]; }]; self.loginBtn.rac_command = self.loginCommand;
  17. 17. Resources https://github.com/ReactiveCocoa/ReactiveCocoa https://pinboard.in/u:andrewsardone/t:ReactiveCocoa
  18. 18. Questions? http://www.smartlogic.io http://www.twitter.com/smartlogic http://www.github.com/smartlogic http://www.facebook.com/smartlogic
  1. A particular slide catching your eye?

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

×