如何在iOS UISearchBar中限制搜索(基于打字速度)? [英] How to throttle search (based on typing speed) in iOS UISearchBar?

查看:228
本文介绍了如何在iOS UISearchBar中限制搜索(基于打字速度)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有UISearchDisplayController的UISearchBar部分,用于显示本地CoreData和远程API的搜索结果。
我想要实现的是远程API搜索的延迟。目前,对于用户键入的每个字符,发送请求。但是如果用户输入特别快,那么发送许多请求就没有意义:等到他停止输入会有所帮助。
有没有办法实现这一目标?

I have a UISearchBar part of a UISearchDisplayController that is used to display search results from both local CoreData and remote API. What I want to achieve is the "delaying" of the search on the remote API. Currently, for each character typed by the user, a request is sent. But if the user types particularly fast, it does not make sense to send many requests: it would help to wait until he has stopped typing. Is there a way to achieve that?

阅读文档建议等到用户明确点击搜索,但我发现在我的情况下并不理想。

Reading the documentation suggests to wait until the users explicitly taps on search, but I don't find it ideal in my case.


性能问题。如果搜索操作可以快速执行
,则可以通过在
委托对象上实现searchBar:textDidChange:方法,在用户输入
时更新搜索结果。但是,如果搜索操作需要更多时间,那么
应该等到用户在searchBarSearchButtonClicked:方法中开始
搜索之前点击搜索按钮。始终执行
搜索操作后台线程以避免阻塞主
线程。这可以让您的应用在用户运行
时响应用户,并提供更好的用户体验。

Performance issues. If search operations can be carried out very rapidly, it is possible to update the search results as the user is typing by implementing the searchBar:textDidChange: method on the delegate object. However, if a search operation takes more time, you should wait until the user taps the Search button before beginning the search in the searchBarSearchButtonClicked: method. Always perform search operations a background thread to avoid blocking the main thread. This keeps your app responsive to the user while the search is running and provides a better user experience.

发送大量请求到API不是本地性能的问题,而只是避免远程服务器上的请求率太高。

Sending many requests to the API is not a problem of local performance but only of avoiding too high request rate on the remote server.

谢谢

推荐答案

感谢此链接,我发现了一种非常快速和干净的方法。与Nirmit的答案相比,它没有加载指标,但它在代码行数方面获胜,并且不需要额外的控制。我首先将 dispatch_cancelable_block.h 文件添加到我的项目中(来自此回购 ),然后定义以下类变量: __ block dispatch_cancelable_block_t searchBlock;

Thanks to this link, I found a very quick and clean approach. Compared to Nirmit's answer it lacks the "loading indicator", however it wins in terms of number of lines of code and does not require additional controls. I first added the dispatch_cancelable_block.h file to my project (from this repo), then defined the following class variable: __block dispatch_cancelable_block_t searchBlock;.

我的搜索代码现在如下所示:

My search code now looks like this:

- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
    if (searchBlock != nil) {
        //We cancel the currently scheduled block
        cancel_block(searchBlock);
    }
    searchBlock = dispatch_after_delay(searchBlockDelay, ^{
        //We "enqueue" this block with a certain delay. It will be canceled if the user types faster than the delay, otherwise it will be executed after the specified delay
        [self loadPlacesAutocompleteForInput:searchText]; 
    });
}

注:


  • loadPlacesAutocompleteForInput LPGoogleFunctions 的一部分库

  • searchBlockDelay @implementation :

静态CGFloat searchBlockDelay = 0.2;

static CGFloat searchBlockDelay = 0.2;

这篇关于如何在iOS UISearchBar中限制搜索(基于打字速度)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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