29. Map
// set the label to always be equal to the formatted
// string of “12 units”, where 12 is whatever the
// current value of self.total
RACSignal *signal = RACObserve(self, total);
RAC(self, totalLabel.text) = [signal map:^id(NSNumber
*total) {
[NSString stringWithFormat:@"%i units", total];
}];
30. Filter
// only set total to the even numbers in
// the stream
RAC(self, total) = [RACSignal(self, cell1)
filter:^BOOL(NSNumber *number) {
return @([number intValue] % 2 == 0);
}];
33. Sequencing
// when signal 1 completes, do signal 2
[[signal doNext:^(id x) {
NSLog(@"value: %@", x);
}]
then:^RACSignal *{
return signal2;
}];
34. Merging Signals
// creates a new signal that will send the
// values of both signals, complete when both
// are completed, and error when either errors
[RACSignal merge:@[signal1, signal2]];
37. Flattening & Mapping
// creates multiple signals of work which
// are automatically recombined, or in other words
// it maps each letter to a signal using
// saveEntriesForLetter: and then it merges them all.
letters = @[@“a”, @“b”, @“c”]
[[letters
flattenMap:^(NSString *letter) {
return [database saveEntriesForLetter:letter];
}]
subscribeCompleted:^{
NSLog(@"All database entries saved successfully.");
}];
39. What is a “side effect?”
• logging
• making a network request
• update the UI
• changing some state somewhere
40. Subscriptions
[signal subscribeNext:^(id x) {
// do something with each value
} error:^(NSError *error) {
// do something with errors
} completed:^{
// do something with completed
}];
41. Inject Side Effects
[signal doCompleted:^{
// do some side effect after
}];
!
[signal doNext:^(id x) {
// some side effect here
}];
!
[signal doError:^(NSError *error) {
// handle error
}];
48. Scenario
• I want to run three networking calls and then when
they are all done do something
• I want to MERGE three signals and THEN do
something.
52. Other Cool Methods
• -throttle:
• -takeUntil: can be used to automatically dispose of a
subscription when an event occurs (like a "Cancel"
button being pressed in the UI).
• -setNameWithFormat: for debugging
• -logNext, -logError, -logCompleted, -logAll automatically
log signal events as they occur, and include the name
of the signal in the messages. This can be used to
conveniently inspect a signal in real-time.
53.
54. Your hammer
99% of the time
• -subscribeNext:error:completed:
• -doNext: / -doError: / -doCompleted:
• -map:
• -filter:
• -concat:
• -flattenMap:
• -then:
• +merge:
• +combineLatest:reduce:
• -switchToLatest:
59. Github’s Obj-C API Lib
https://github.com/octokit/octokit.objc
!
(and Instructure’s API Lib modeled after it)
https://github.com/instructure/canvaskit