REACTIVE COCOA
Trilha iOS
Carlos Vinícius Real Guilherme Endres
Software Engineer Software Engineer
ReactiveCocoa?
ISSO NÃO VAI DAR CERTO…
RAC
IOS
ReactiveCocoa?
Functional Reactive
Programming
Reactive Cocoa
Reactive Programming
+
Functional Programming
Reactive Cocoa
Reactive Programming
+
Functional Programming
Reactive Cocoa
Reactive Programming
Reactive Programming
+
Functional Programming
Reactive Cocoa
Functional Programming
Imperativo vs Declarativo
FRP | Imperativo
Como
var strongBeers = [Beer]();
for beer in beers {
if beer.alcohoolPercent > 6 {
strongBeers.append(beer)
}
}
FRP | Declarativo
O que
beers.filter{ $0.alcohoolPercent > 6 }
SIGNALS 

& 

SIGNALS PRODUCERS
Weiss
map
filter
RedAle
http://neilpa.me/rac-marbles
EVENTS
Events
Next
Completed
Failed
Interrupted
CallBack blocks
KVO
Actions
Delegate
NSNOTIFICATION
CODE
CODE
Swift
ReactiveCocoa4
MVVM
BUTTON SIGNAL
let buttonSignal: RACSignal = enterButton.rac_signalForControlEvents(
UIControlEvents.TouchUpInside)
USERVIEWCONTROLLER | BUTTON SIGNAL
let buttonSignal: RACSignal = enterButton.rac_signalForControlEvents(
UIControlEvents.TouchUpInside)
let buttonSignalProducer: SignalProducer = buttonSignal
.toSignalProducer()
.observeOn(UIScheduler())
USERVIEWCONTROLLER | BUTTON SIGNAL PRODUCER
OPERATORS
buttonSignalProducer
.flatMap(
transform:)
USERVIEWCONTROLLER | OPERATORS
buttonSignalProducer
.flatMap(FlattenStrategy.Latest,
transform: userViewModel.saveUser)
USERVIEWCONTROLLER | OPERATORS
buttonSignalProducer
.flatMap(FlattenStrategy.Latest,
transform: userViewModel.saveUser)
.startWithNext { username in
self.performSegueWithIdentifier("beerControllerSegue", sender: self)
}
USERVIEWCONTROLLER | OPERATORS
MUTABLE PROPERTY
var quantity = MutableProperty<Int>(0)
var userImage = MutableProperty<UIImage?>.init(defaultImage)
BEERVIEWMODEL | MUTABLE PROPERTIES
BINDING<~
beerViewModel.quantity <~ beerQuantityProducer
BEERVIEWCONTROLLER | BINDING
MÉTODOS
func imageForBeer(quantity: Int) -> SignalProducer<UIImage?, NoError> {
}
BEERVIEWMODEL | MÉTODOS
func imageForBeer(quantity: Int) -> SignalProducer<UIImage?, NoError> {
return SignalProducer { observe, disposable in
}.delay(1, onScheduler: QueueScheduler.mainQueueScheduler)
}
BEERVIEWMODEL | MÉTODOS
func imageForBeer(quantity: Int) -> SignalProducer<UIImage?, NoError> {
return SignalProducer { observe, disposable in
guard let image: UIImage = imageForQuantity(quantity) else {
print("Failed to get image for quantity (quantity)")
return
}
}.delay(1, onScheduler: QueueScheduler.mainQueueScheduler)
}
BEERVIEWMODEL | MÉTODOS
func imageForBeer(quantity: Int) -> SignalProducer<UIImage?, NoError> {
return SignalProducer { observe, disposable in
guard let image: UIImage = imageForQuantity(quantity) else {
print("Failed to get image for quantity (quantity)")
return
}
observe.sendNext(image)
}.delay(1, onScheduler: QueueScheduler.mainQueueScheduler)
}
BEERVIEWMODEL | MÉTODOS
userImage <~ quantity.producer
BEERVIEWMODEL | BINDING
userImage <~ quantity.producer
.skipRepeats()
BEERVIEWMODEL | BINDING
userImage <~ quantity.producer
.skipRepeats()
.flatMap(FlattenStrategy.Latest, transform: imageForBeer)
BEERVIEWMODEL | BINDING
personDrunkImage.rac_image <~ beerViewModel.userImage
BEERVIEWCONTROLLER | BINDING
UNIT TEST
let userViewModel: UserViewModel = UserViewModel()
UNIT TEST | SUCESSO
let userViewModel: UserViewModel = UserViewModel()
userViewModel.username.value = "Skywalker"
UNIT TEST | SUCESSO
let userViewModel: UserViewModel = UserViewModel()
userViewModel.username.value = "Skywalker"
userViewModel.saveUser().on(
).start()
UNIT TEST | SUCESSO
let userViewModel: UserViewModel = UserViewModel()
userViewModel.username.value = "Skywalker"
userViewModel.saveUser().on(
completed: {
successExpectation.fulfill()
}, failed: { error in
XCTFail()
}).start()
UNIT TEST | SUCESSO
let userViewModel: UserViewModel = UserViewModel()
userViewModel.username.value = "Darth Vader"
userViewModel.saveUser().on(
completed: {
XCTFail()
}, failed: { error in
successExpectation.fulfill()
}).start()
UNIT TEST | FALHA
PROBLEMAS
https://github.com/username/project
Signals
Signal Producer
Map
Functional programming
"Se liga no mestiço, na batida do cavaco"?
Reactive
Reduce
Links úteis | Reactive Cocoa
http://reactivecocoa.io/philosophy.html
https://github.com/ReactiveCocoa/ReactiveCocoa
http://www.sprynthesis.com/2014/12/06/reactivecocoa-mvvm-
introduction/
https://nomothetis.svbtle.com/an-introduction-to-reactivecocoa
http://neilpa.me/rac-marbles
https://github.com/gfendres/ractdc2016
CVINICIUSREAL@GMAIL.COM
Carlos Vinícius Real Guilherme Endres
GFENDRES@GMAIL.COM

ReactiveCocoa - TDC 2016