text Throttling Searchbar ios swift

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了text Throttling Searchbar ios swift相关的知识,希望对你有一定的参考价值。

class SearchViewController: UIViewController, UISearchBarDelegate {
    // We keep track of the pending work item as a property
    private var pendingRequestWorkItem: DispatchWorkItem?

    func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
        // Cancel the currently pending item
        pendingRequestWorkItem?.cancel()

        // Wrap our request in a work item
        let requestWorkItem = DispatchWorkItem { [weak self] in
            self?.resultsLoader.loadResults(forQuery: searchText)
        }

        // Save the new work item and execute it after 250 ms
        pendingRequestWorkItem = requestWorkItem
        DispatchQueue.main.asyncAfter(deadline: .now() + .milliseconds(250),
                                      execute: requestWorkItem)
    }
}

// Written by John sundell https://www.swiftbysundell.com/posts/a-deep-dive-into-grand-central-dispatch-in-swift

以上是关于text Throttling Searchbar ios swift的主要内容,如果未能解决你的问题,请参考以下文章

如何访问 searchBar 的明文按钮?

javascript防抖(Debouncing)和节流阀(Throttling)

javascript防抖(Debouncing)和节流阀(Throttling)

节流(Throttling)和去抖(Debouncing)详解

python throttling.py

限流Throttling