Reactivecocoa+Autocompletion (with delay)
@implementation BootStrap
- (void) init {
if (self = [super init]) {
@weakify(self);
RAC(self, result) = [[[[self.textfield.rac_textSignal
ignore:@""]
distinctUntilChanged]
map:^RACStream *(NSString *text) {
@strongify(self);
return [[[self jsonRequest:text]
delay:0.4f]
logAll];
}]
switchToLatest];
}
- (RACSignal *) jsonRequest: (NSString *)parameter {
return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
NSOperation *operation = [[AFHTTPRequestOperationManager manager] GET:@"http://int.dpool.sina.com.cn/iplookup/iplookup.php"
parameters:@{@"ip": parameter, @"format": @"json"}
success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSString *next = [NSString stringWithFormat:@"%@, %@", parameter, responseObject];
[subscriber sendNext:next];
[subscriber sendCompleted];
}
failure:^(AFHTTPRequestOperation *operation, NSError *error) {
[subscriber sendError:error];
}];
return [RACDisposable disposableWithBlock:^{
[operation cancel];
}];
}];
}