Donner le goût du ReactiveCocoa. L’objectif de la présentation étant de nous expliquer comment migrer du code lié au KVO vers une implémentation ReactiveCocoa. Cette approche du KVO par ReactiveCocoa permet de comprendre très facilement les bases et l’intérêt de ce nouveau framework et de s’y mettre facilement et progressivement.
6. Simple binding
Typical binding
RAC(self, userName) = RACObserve(self, userManager.currentUser.lastName);
userName is bound to userManager.currentUser.lastName.
But what if userManager or currentUser is nil?
RAC(self, userName, @“none”) = RACObserve(self, userManager.currentUser.lastName);
Bonus: Don’t care whether objects in observed
keypath are already allocated
15. Parameters as id
Most RAC methods return RACSignal and use
id as parameters
[RACObserve(self, userManager.currentUser) subscribeNext:^(id x) {
NSLog(@"%@", ((User *)x).lastName);
}];
☟
[RACObserve(self, userManager.currentUser) subscribeNext:^(User *user) {
NSLog(@"%@", user.lastName);
}];
16. Weakify/Strongify
@weakify(self)
[RACObserve(self, userManager.currentUser) subscribeNext:^(User *user) {
@strongify(self)
[self userChanged:user];
}];
@weakify creates a new weakly referenced variable of the same type you
pass in and assigns the original value to it
@strongify creates a variable that matches the original variable but it exists
in the local scope and assigns to it the variable created by @weakify
17. Weakify/Strongify
_weak id weakSelf = self;
[RACObserve(self, userManager.currentUser) subscribeNext:^(User *user) {
id strongSelf = weakSelf;
[strongSelf userChanged:user];
}];
@strongify in each async block
No need for several @weakify(self)
19. Pros
Get rid of boilerplate code
Less error prone
(missing automaticallyNotifiesObserversOf<Property>?)
Compatible with property refactoring (no more
reference to keypath as string)
If you change a property name manually, RAC code won’t compile
anymore.
Configuration is much easier, especially with multilevel
keypath
Memory management is rightly done out of the box
Start to think functional, prepare for Swift
20. Cons
All values are object, BOOL are passed as
NSNumber
=> isAdmin.boolValue 😢
Don’t forget deliverOnMainThread for UIKit
components
21. Warnings
Don’t use RAC on intensive property change
for example on Player.currentTime
Don’t look at the stack!
22. Warnings
Don’t use RAC on intensive property change
for example on Player.currentTime
Don’t look at the stack!
23.
24. CocoaHeads Paris
KVO with ReactiveCocoa
Philippe Converset
pconverset@autresphere.com
@autresphere
www.autresphere.com
www.kidzik.com
github.com/autresphere