iOS:等到用户完成输入然后发送请求 [英] iOS: Wait until user finishes typing and then send request

查看:94
本文介绍了iOS:等到用户完成输入然后发送请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的 UITableView UISearchBar / UISearchDisplayController ,它通过使用RKObjectManager从远程 ElasticSearch 服务器获取结果。
我遇到的问题是,如果用户快速键入或者术语有点大,则有几个请求失败,有时我无法得到结果。

I have a simple UITableView with UISearchBar/UISearchDisplayController that fetches results from remote ElasticSearch server by using RKObjectManager. Problem I have is that if user types to quickly or the term is a bit bigger several of requests fail and sometimes I don't get the results.

是否可以选择等到用户停止输入,然后发送请求
,而不是按照他输入的每个字母发送请求?

Is there an option to wait until user has stopped typing and then send request instead of sending request per each letter he types in?

推荐答案

在发送请求之前添加一个小延迟,然后在用户继续输入时取消该延迟请求

Add a small delay before sending the request, and then cancel that delayed request if the user continues typing

- (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 its noticeable to the user that there is a delay, too short and you have the same problem as now

这篇关于iOS:等到用户完成输入然后发送请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆