搜索框或者UITextField使用ReactiveCocoa

有時候我們使用搜索框或者UITextField,要在輸入的時候進行處理,或者請求數據,但是不能馬上請求,在要停止輸入一段時間之后再進行請求,不然會浪費資源去一直請求數據。這個時候使用ReactiveCocoa再適合不過。
[[[[[[[self requestAccessToTwitterSignal]
then:^RACSignal * _Nonnull{
@strongify(self)
return self.searchText.rac_textSignal;
}]
filter:^BOOL(NSString * text) {
@strongify(self)
return [self isValidSearchText:text];
}] throttle:0.5]
flattenMap:^__kindof RACSignal * _Nullable(NSString * text) {
@strongify(self)
return [self signalForSearchWithText:text];
}] deliverOn:[RACScheduler mainThreadScheduler]]
subscribeNext:^(NSDictionary *jsonSearchResult) {
NSLog(@"access granted %@",jsonSearchResult);
NSArray *statuses = jsonSearchResult[@"statuses"];
NSArray *tweets = [statuses linq_select:^id(id tweet) {

         return [RWTweet tweetWithStatus:tweet];
     }];
     [self.resultsViewController displayTweets:tweets];
     
 } error:^(NSError * _Nullable error) {
     NSLog(@"access error is %@",error);
 }];

其中的throttle就是等待0.5秒沒有變化就會起作用。

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容