IOS: Wait for the user to finish typing and then send a request

I have a simple UITableView with a UISearchBar / UISearchDisplayController that retrieves results from a remote ElasticSearch server using RKObjectManager. The problem is that if the user types quickly or the term is a bit larger, several queries fail, and sometimes I get no results.

Is there an option to wait until the user stops typing , and then send a request instead of sending a request for each letter that he enters?

+6
source share
2 answers

Add a slight delay before sending the request and then cancel this request with a delay if the user continues to print

- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText { [NSObject cancelPreviousPerformRequestsWithTarget:self]; [self performSelector:@selector(sendSearchRequest) withObject:searchText afterDelay:0.1f]; } 

You may need to adjust the delay time. too long, and it’s noticeable to the user that there is a delay, too short, and you have the same problem as now.

+22
source

This is for the quick version.

 NSObject.cancelPreviousPerformRequestsWithTarget(self) self.performSelector("searchForText:", withObject: searchString, afterDelay: 0.5) 
+7
source

Source: https://habr.com/ru/post/971606/


All Articles