I am try to follow tutorial regarding to ReactiveCocoa from Ray, but somehow the filter
function is working since it always goes down to the subscribeNext
although I debugged that filter
function does go with the return @NO
branch.
#import <Accounts/Accounts.h>
#import <Social/Social.h>
#import <ReactiveCocoa/ReactiveCocoa.h>
#import "SearchViewController.h"
typedef NS_ENUM(NSInteger, RWTwitterInstantError) {
RWTwitterInstantErrorAccessDenied,
RWTwitterInstantErrorNoTwitterAccounts,
RWTwitterInstantErrorInvalidResponse
};
static NSString * const RWTwitterInstantDomain = @"TwitterInstant";
@interface SearchViewController ()
{
RACDisposable *requestTwiiterSubscription;
}
@property (strong, nonatomic) ACAccountStore *accountStore;
@property (strong, nonatomic) ACAccountType *twitterAccountType;
@property (weak, nonatomic) IBOutlet UISearchBar *searchBar;
@property (strong, nonatomic) UITextField *searchBarTextField;
@end
@implementation SearchViewController
- (UITextField *)searchBarTextField {
if (!_searchBarTextField) {
for (UIView *view in self.searchBar.subviews) {
for (id deeperView in view.subviews) {
if ([deeperView isKindOfClass:[UITextField class]]) {
_searchBarTextField = deeperView;
}
}
}
}
return _searchBarTextField;
}
- (void)viewDidLoad {
[super viewDidLoad];
self.searchBar.text = @"Co";
__weak SearchViewController *weakSelf = self;
self.accountStore = [[ACAccountStore alloc] init];
self.twitterAccountType = [self.accountStore
accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter];
/**
* The then method waits until a completed event is emitted, then subscribes to the signal returned by its block parameter.
* This effectively passes control from one signal to the next.
*/
requestTwiiterSubscription = [[[[self requestAccessToTwitterSignal]
then:^RACSignal *{
return weakSelf.searchBarTextField.rac_textSignal;
}]
filter:^BOOL(NSString *textString) {
if (textString.length >= 3) {
return @YES;
}
return @NO;
}]
subscribeNext:^(id x) {
NSLog(@"%@", x);
} error:^(NSError *error) {
NSLog(@"An error occurred: %@", error);
}];
}
- (void)dealloc {
[requestTwiiterSubscription dispose];
}
- (RACSignal *)requestAccessToTwitterSignal {
// 1 - define an error
NSError *accessError = [NSError errorWithDomain:RWTwitterInstantDomain
code:RWTwitterInstantErrorAccessDenied
userInfo:nil];
// 2 - create the signal
return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
// 3 - request access to twitter
[self.accountStore
requestAccessToAccountsWithType:self.twitterAccountType
options:nil
completion:^(BOOL granted, NSError *error) {
// 4 - handle the response
if (!granted) {
[subscriber sendError:accessError];
} else {
[subscriber sendNext:nil];
[subscriber sendCompleted];
}
}];
return nil;
}];
}
@end
You have the wrong return values in the filter. You want:
requestTwiiterSubscription = [[[[self requestAccessToTwitterSignal]
then:^RACSignal *{
return weakSelf.searchBarTextField.rac_textSignal;
}]
filter:^BOOL(NSString *textString) {
if (textString.length >= 3) {
return YES;
}
return NO;
}]
subscribeNext:^(id x) {
NSLog(@"%@", x);
} error:^(NSError *error) {
NSLog(@"An error occurred: %@", error);
}];
You were returning NSNumber
objects instead of BOOL
values.