搜索框或者UITextField使用ReactiveCocoa

有時(shí)候我們使用搜索框或者UITextField,要在輸入的時(shí)候進(jìn)行處理,或者請求數(shù)據(jù),但是不能馬上請求,在要停止輸入一段時(shí)間之后再進(jìn)行請求,不然會(huì)浪費(fèi)資源去一直請求數(shù)據(jù)。這個(gè)時(shí)候使用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秒沒有變化就會(huì)起作用。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容