Skip to content

Instantly share code, notes, and snippets.

@prabirshrestha
Created August 2, 2012 13:53
Show Gist options
  • Star 18 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save prabirshrestha/3237267 to your computer and use it in GitHub Desktop.
Save prabirshrestha/3237267 to your computer and use it in GitHub Desktop.
Reactive Cocoa examples
@synthesize btnSearch = _btnSearch;
RACSubscribable *subscribable = [self.btnSearch rac_subscribableForControlEvents:UIControlEventTouchUpInside];
[s subscribeNext:^(id x) {
NSLog(@"touched");
}];
or
[[self.btnSearch rac_subscribableForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
NSLog(@"touched2");
}];
RACEventTrampoline *trampoline = [[RACEventTrampoline alloc] init];
[trampoline setDelegateMethod:@selector(textFieldShouldReturn:)];
Protocol *protocol = @protocol(UITextFieldDelegate);
RACDelegateProxy *proxy = [RACDelegateProxy proxyWithProtocol:protocol andDelegator:self.textField1];
[proxy addTrampoline:trampoline];
[self.textField1 setDelegate:(id<UITextFieldDelegate>)proxy];
[trampoline.subject
subscribeNext:^(UITextField *x) {
[x resignFirstResponder];
NSLog(@"subscribe next %@", x.text);
}];
@synthesize firstName = _firstName;
@synthesize txtFirstName = _txtFirstName;
[RACAbleSelf(self.firstName) subscribeNext:^(id x) { [self firstNameChanged:x]; }];
[self rac_bind:RAC_KEYPATH_SELF(self.firstName) to:self.txtFirstName.rac_textSubscribable];
- (void) firstNameChanged:(id)firstName {
NSLog(@"changed: %@", firstName);
}
or
[[[RACAbleSelf(self.firstName)
select:^id(NSString *x) {
return [x stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
}]
where:^BOOL(NSString *x) {
return x.length > 0;
}]
subscribeNext:^(id x) { [self firstNameChanged:x]; }];
[self rac_bind:RAC_KEYPATH_SELF(self.firstName) to:self.txtFirstName.rac_textSubscribable];
@property (strong, nonatomic) IBOutlet UITextField *txtFirstName;
[[[[[[[[[[self.txtFirstName
rac_subscribableForControlEvents:UIControlEventEditingChanged]
select:^id(UITextField *x) {
return x.text;
}]
where:^BOOL(NSString *x) {
return [x stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]].length > 2;
}]
throttle:.5 /* seconds */]
distinctUntilChanged]
select:^id(NSString *x) {
return [self searchWikipedia:x];
}]
merge]
select:^id(id x) {
return [x objectAtIndex:1];
}]
where:^BOOL(NSArray *x) {
return x.count > 0;
}]
subscribeNext:^(id x) {
NSLog(@"---");
NSLog(@"x: %@", x);
} error:^(NSError *error) {
NSLog(@"%@", error);
}];
- (RACAsyncSubject*) searchWikipedia:(NSString*) term {
NSURL *url = [NSURL URLWithString:[NSString
stringWithFormat:@"http://en.wikipedia.org/w/api.php?action=opensearch&format=json&search=%@", term]];
NSURLRequest * request = [NSURLRequest requestWithURL:url];
RACAsyncSubject *subject = [RACAsyncSubject subject];
AFHTTPRequestOperation *operation =
[AFJSONRequestOperation JSONRequestOperationWithRequest:request
success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
[subject sendNext:JSON];
[subject sendCompleted];
} failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) {
NSLog(@"fail");
[subject sendError:error];
}];
[operation start];
return subject;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment