ReactiveCocoa provides a common interface for handling asynchronous events from different sources like UI controls, network requests, and notifications. It uses signals that emit events to represent asynchronous data over time. Operations like filtering, mapping, and combining signals allow defining reactive workflows. Swift's support for functional programming makes it a good fit for the declarative style of ReactiveCocoa.
13. Get Reactive
let textSignal: RACSignal =
usernameTextField.rac_textSignal()
textSignal.subscribeNext {
(text: AnyObject!) -> Void in
let textString = text as String
println(textString)
}
15. Objective-C Friction
let textSignal: RACSignal =
usernameTextField.rac_textSignal()
textSignal.subscribeNext {
(text: AnyObject!) -> Void in
let textString = text as String
println(textString)
}
16. Simplified with Swift
func subscribeNextAs<T>(nextClosure:(T) -> ()) -> () {
self.subscribeNext {
textSignal.subscribeNextAs {
(text: String) -> () in
println(text)
}
(next: AnyObject!) -> () in
let nextAsT = next! as T
nextClosure(nextAsT)
}
}
textSignal.subscribeNext {
(text: AnyObject!) -> Void in
let textString = text as String
println(textString)
}
17. Signals
• A signal emits events
• next
• error
• completed
• A signal can have none,
one or more subscribers
textSignal.subscribeNextAs({
(text: String) in
println(text)
}, error: {
(error) in
// ...
}, completed: {
// ...
})
18. Events
Signals can emit none, one or more next events,
optionally followed by either an error or
completed
COMPLETED
NEXT NEXT ERROR
NEXT NEXT NEXT NEXT …
intervals do not
have to be
regular!
19. Signal all things
• Network request
• A single next, followed by a completed
• Large download
• Multiple next events, representing partial data,
followed by completed
• UI control
• An infinite stream of next events
20. filter
let textSignal: RACSignal = usernameTextField.rac_textSignal()
let filteredText = textSignal.filterAs {
(text: NSString) -> Bool in
return text.length > 3
}
filteredText.subscribeNextAs {
(text: String) in
println(text)
}
A filter is a ‘gate’, filtering-out events which do not match
the given condition
21.
22. What exactly are events?
• What does a next event actually look like?
• Anything!
• Signals are an interface for handling
asynchronous events
• The event contents are context dependant
23. map
let textSignal: RACSignal = usernameTextField.rac_textSignal()
let textLength = textSignal.mapAs {
(text: NSString) -> NSNumber in
return text.length
}
textLength.subscribeNextAs {
(length: NSNumber) in
println(length)
}
Transforms each next event (please ignore the types!)
24.
25. Creating a pipeline
let textSignal: RACSignal =
usernameTextField.rac_textSignal()
let textLength = textSignal.mapAs {
(text: NSString) -> NSNumber in
return text.length
}
let filteredText = textLength.filterAs {
(number: NSNumber) -> Bool in
return number > 3
}
filteredText.subscribeNextAs {
(length: NSNumber) in
println(length)
}
26. Fluent syntax
usernameTextField
.rac_textSignal()
.mapAs {
(text: NSString) -> NSNumber in
return text.length
}
.filterAs {
(number: NSNumber) -> Bool in
return number > 3
}
.subscribeNextAs {
(length: NSNumber) in
println(length)
}
65. 39 constants
12 variables
6 outlets (not my fault!)
1 UIWindow
5 UI state variables
66. ReactiveCocoa and Swift
Better Together
@ColinEberhardt
ShinobiControls
TwitterSentiment:
https://github.com/ColinEberhardt/ReactiveSwiftLondon
MVVM with ReactiveCocoa and Swift:
https://github.com/ColinEberhardt/ReactiveSwiftFlickrSearch
Tutorials:
http://www.raywenderlich.com/u/ColinEberhardt