如何去除方法调用?

编程入门 行业动态 更新时间:2024-10-11 05:26:05
本文介绍了如何去除方法调用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试使用 UISearchView 来查询Google地方。这样做,在文本更改调用我的 UISearchBar 时,我正在向google地方发出请求。问题是我宁愿将此调用去抖动,每250毫秒只请求一次,以避免不必要的网络流量。我不想自己写这个功能,但是如果需要,我会的。

I'm trying to use a UISearchView to query google places. In doing so, on text change calls for my UISearchBar, I'm making a request to google places. The problem is I'd rather debounce this call to only request once per 250 ms in order to avoid unnecessary network traffic. I'd rather not write this functionality myself, but I will if I need to.

我发现:gist.github/ShamylZakariya/54ee03228d955f458389 ,但我不太确定如何使用它:

I found: gist.github/ShamylZakariya/54ee03228d955f458389 , but I'm not quite sure how to use it:

func debounce( delay:NSTimeInterval, #queue:dispatch_queue_t, action: (()->()) ) -> ()->() { var lastFireTime:dispatch_time_t = 0 let dispatchDelay = Int64(delay * Double(NSEC_PER_SEC)) return { lastFireTime = dispatch_time(DISPATCH_TIME_NOW,0) dispatch_after( dispatch_time( DISPATCH_TIME_NOW, dispatchDelay ), queue) { let now = dispatch_time(DISPATCH_TIME_NOW,0) let when = dispatch_time(lastFireTime, dispatchDelay) if now >= when { action() } } } }

以下是我尝试使用上述内容的一件事代码:

Here is one thing I've tried using the above code:

let searchDebounceInterval: NSTimeInterval = NSTimeInterval(0.25) func findPlaces() { // ... } func searchBar(searchBar: UISearchBar!, textDidChange searchText: String!) { debounce( searchDebounceInterval, dispatch_get_main_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT), self.findPlaces ) }

结果错误是无法使用类型'的参数列表调用函数(NSTimeInterval,$ T5,() - > ())

如何使用此方法,或者在iOS / Swift中有更好的方法。

How do I use this method, or is there a better way to do this in iOS/Swift.

推荐答案

将此文件放在文件的顶级中,以免混淆Swift的有趣参数名称规则。请注意,我已删除#,以便现在没有参数包含名称:

Put this at the top level of your file so as not to confuse yourself with Swift's funny parameter name rules. Notice that I've deleted the # so that now none of the parameters have names:

func debounce( delay:NSTimeInterval, queue:dispatch_queue_t, action: (()->()) ) -> ()->() { var lastFireTime:dispatch_time_t = 0 let dispatchDelay = Int64(delay * Double(NSEC_PER_SEC)) return { lastFireTime = dispatch_time(DISPATCH_TIME_NOW,0) dispatch_after( dispatch_time( DISPATCH_TIME_NOW, dispatchDelay ), queue) { let now = dispatch_time(DISPATCH_TIME_NOW,0) let when = dispatch_time(lastFireTime, dispatchDelay) if now >= when { action() } } } }

现在,在您的实际课程中,您的代码将如下所示:

Now, in your actual class, your code will look like this:

let searchDebounceInterval: NSTimeInterval = NSTimeInterval(0.25) let q = dispatch_get_main_queue() func findPlaces() { // ... } let debouncedFindPlaces = debounce( searchDebounceInterval, q, findPlaces )

现在 debouncedFindPlaces 是一个你可以调用的函数,你的 findPlaces 将不会执行,除非 delay 自您上次调用它以来已经过去了。

Now debouncedFindPlaces is a function which you can call, and your findPlaces won't be executed unless delay has passed since the last time you called it.

更多推荐

如何去除方法调用?

本文发布于:2023-11-30 13:47:29,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1650210.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:方法

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!