Created
June 5, 2013 00:26
-
-
Save seanhess/5710745 to your computer and use it in GitHub Desktop.
ReactiveCocoa is better than KVO and delegates
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// TestViewController.m | |
// iOSDemo | |
// | |
// Created by Sean Hess on 6/4/13. | |
// Copyright (c) 2013 GitHub, Inc. All rights reserved. | |
// | |
#import "TestViewController.h" | |
#import "User.h" | |
#import <ReactiveCocoa/ReactiveCocoa.h> | |
@interface TestViewController () <UserDelegate> | |
@property (nonatomic, strong) User * user; | |
@property (nonatomic, strong) UIButton * logoutButton; | |
@property (nonatomic, strong) UIButton * loginButton; | |
@end | |
@implementation TestViewController | |
- (void)viewDidLoad | |
{ | |
[super viewDidLoad]; | |
// 1. DELEGATES | |
// problems: | |
// - only one delegate allowed | |
// - I don't actually "own" user | |
// - events are a strange way to do this. What if there is more state? | |
// - has user even been set yet?? | |
self.user.delegate = self; | |
// 2. NOTIFICATIONS (basically the same | |
// - same problems | |
// - (+) now multiple listeners | |
// - but this funky global notification center. | |
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(userDidLogin) name:@"login" object:self.user]; | |
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(userDidLogut) name:@"login" object:self.user]; | |
// 3. KVO | |
// - data driven | |
// - error prone: "loggedIn" string | |
// - bad syntax: in 3 places. Big if/else statement | |
[self.user addObserver:self forKeyPath:@"loggedIn" options:NSKeyValueObservingOptionNew context:nil]; | |
// 4. ReactiveCocoa | |
// - user doesn't even have to be set!! | |
// - great code locality | |
// - don't have to use the string name of the property | |
// - LOTS more great features | |
[RACAble(self.user.loggedIn) subscribeNext:^(id x) { | |
[self setButtonsBasedOnLogin]; | |
}]; | |
} | |
// DELEGATES | |
- (void)userDidLogin { | |
self.logoutButton.hidden = NO; | |
self.loginButton.hidden = YES; | |
} | |
- (void)userDidLogout { | |
self.logoutButton.hidden = YES; | |
self.loginButton.hidden = NO; | |
} | |
// 3. IMPROVED | |
- (void)setButtonsBasedOnLogin { | |
self.logoutButton.hidden = self.user.loggedIn; | |
self.loginButton.hidden = !self.user.loggedIn; | |
} | |
-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context { | |
if ([keyPath isEqualToString:@"loggedIn"] && object == self.user) { | |
[self setButtonsBasedOnLogin]; | |
} | |
} | |
@end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// User.h | |
// iOSDemo | |
// | |
// Created by Sean Hess on 6/4/13. | |
// Copyright (c) 2013 GitHub, Inc. All rights reserved. | |
// | |
#import <Foundation/Foundation.h> | |
@protocol UserDelegate <NSObject> | |
-(void)userDidLogin; | |
-(void)userDidLogout; | |
@end | |
@interface User : NSObject | |
@property (weak, nonatomic) id<UserDelegate>delegate; | |
@property (nonatomic) BOOL loggedIn; | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment