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

查看:21
本文介绍了如何在 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 文件添加到我的项目中(来自 这个 repo),然后定义了以下内容类变量:__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]; 
    });
}

注意事项:

  • loadPlacesAutocompleteForInputLPGoogleFunctions 库的一部分
  • searchBlockDelay@implementation 之外定义如下:

  • The loadPlacesAutocompleteForInput is part of the LPGoogleFunctions library
  • searchBlockDelay is defined as follows outside of the @implementation:

静态 CGFloat searchBlockDelay = 0.2;

static CGFloat searchBlockDelay = 0.2;

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

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