延迟UISearchbar解析 [英] Delay UISearchbar parsing
问题描述
我的应用程序中有一个UISearchbar.这是一种动态搜索,当用户输入文本时,将通过远程API调用(我认为是通过REST)搜索远程数据库.
I have a UISearchbar in my app. This is a dynamic search and as the user enters text, a remote database is searched via a remote API call (I think it is through REST).
随着用户键入,表视图将动态刷新.我正在使用NSXMLParser解析XML结果.(因此3个委托方法; didStartElement,didEndElement)
The table view gets refreshed dynamically, as the user types. I am using NSXMLParser for parsing the XML results. (so 3 delegate methods; didStartElement, didEndElement)
在某些情况下,结果中显示重复的条目例如如果用户键入了YAH,它将显示YAHOO 3-4次.我不确定为什么.
In some cases, there are duplicate entries shown in the results e.g. If user has typed YAH, it shows YAHOO 3-4 times. I'm not sure why.
如何减少解析次数,或如何延迟解析,以使它不会要求用户输入/删除的每个字符.
How can I reduce the number of times the parsing is done, or how to delay the parsing, so that it does not make a request for every character entered/deleted by the user.
我认为这可能会解决问题.
This, I am assuming, might fix the problem.
推荐答案
您可以做的一件事是在发送远程API调用之前引入延迟,而不是为每个字符发送一个查询.
One thing you can do is introduce a delay before you send off the remote API call, instead of sending one query for every character.
// Whenever UISearchbar text changes, schedule a lookup
- (void)searchBar:(UISearchBar *)theSearchBar textDidChange:(NSString *)text {
// cancel any scheduled lookup
[NSObject cancelPreviousPerformRequestsWithTarget:self];
// start a new one in 0.3 seconds
[self performSelector:@selector(doRemoteQuery) withObject:nil afterDelay:0.3];
}
这篇关于延迟UISearchbar解析的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!