如何在 iOS swift 2 中为 uisearchbar 过滤大数组 [英] How to filter large array in iOS swift 2 for uisearchbar

查看:35
本文介绍了如何在 iOS swift 2 中为 uisearchbar 过滤大数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 UISearchBar 在一个数组中有超过 80000 个元素,我必须根据用户输入过滤这个数组.

I am having a UISearchBar with more than 80000 elements in an array and I have to filter this array according to the user input.

但是在搜索视图中输入时,它的工作速度非常慢,这意味着在键盘中输入值需要花费太多时间.

But while typing in search view its working very slow means its taking too much time for typing values in keyboard.

func searchBar(searchBar: UISearchBar, textDidChange searchText: String) {

    if searchText.characters.count == 0 {
        searchActive = false
    } else {
        searchActive = true;
        filtered.removeAllObjects()

        dispatch_to_background_queue {
            for sumber in self.data {
                let nameRange: NSRange = sumber.rangeOfString(searchText, options: [NSStringCompareOptions.AnchoredSearch,NSStringCompareOptions.CaseInsensitiveSearch])
                if nameRange.location != NSNotFound {
                    self.filtered.addObject(sumber)
                }
            }//end of for

            self.dispatch_to_main_queue {
                /* some code to be executed on the main queue */
                self.tableView.reloadData()
            }
        } //end of dispatch
    }
}

func dispatch_to_main_queue(block: dispatch_block_t?) {
    dispatch_async(dispatch_get_main_queue(), block!)
}

func dispatch_to_background_queue(block: dispatch_block_t?) {
    let q = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)
    dispatch_async(q, block!)
}

推荐答案

我想补充几点.

您似乎已经在进行异步处理,这很棒.它不会使搜索更快,但该应用程序会保持响应.考虑使其可停止.如果用户键入三个字母,您将排队三个搜索,并在最后一次运行完成后才能获得相关结果.这可以使用某种在搜索中检查的布尔停止标志来完成.如果开始新的搜索,先杀死旧的.

You already seem to do async processing, which is great. It won't make the search faster, but the app keeps responsive. Consider making it stoppable. If the user types three letters, you will queue up three searches and will get the relevant results only after the last run finished. This could be done using some sort of a boolean stop flag that gets checked within the search. If a new search is started, kill the old one first.

显示部分结果.用户不会同时观看数千个单元格,而只会观看前 20 个左右的单元格.根据您输入和输出的顺序,这可能很容易做到,而且速度极快.

Show partial results. The user won't be watching at thousands of cells at once, but only at the first 20 or so. Depending on the order of your input and output, this may be very easy to do and fast as hell.

以您之前的搜索为基础.只有在搜索A"(或b",如果搜索没有锚定)成功的情况下,搜索Ab"才会成功.因此,如果上次搜索是当前搜索的子字符串,则将上次搜索的输出数组作为输入.显然,在这里停止搜索时要小心.

Build on your previous search. Searching for "Ab" will only be successful if searching for "A" (or "b" for that matter if the search wasn't anchored) was successful at well. So if your last search was a substring from your current search, take the output array of your previous search as an input. Obviously, be careful with stopped searches here.

检查性能是否真的一样糟糕.您是否在开启优化的情况下运行?调试模式可能会慢很多,但这没关系.

Check if performance is really as bad. Do you run with optimizations switched on? The debug mode might be considerable slower, but that wouldn't matter.

数据从哪里来?这是要保留在内存中的大量数据.如果它来自数据库,使用数据库函数可能会更容易(并且上面的大多数词仍然符合).

Where does the data come from? That's a rather huge amount of data to keep around in memory. If it's coming from a database, using database functions might be easier (and most words above still comply).

还是太慢?索引您的数据集.如果您预先知道哪些元素包含A",则所需搜索的数量可能会显着下降.你已经有了第一次搜索的结果

Still too slow? Index your data set. If you know upfront which elements contain "A", the number of needed searches could drop significantly. And you'd have the results for the first search already

当您使用锚定搜索时,处理排序数组可以提供更好的性能特征.只需使用二分搜索找到搜索词的第一个和最后一个元素并使用该范围.也许甚至没有复制到新数组中.这种方法将一些工作量放在首位(甚至可能在用户开始输入之前).如果您的搜索数据在较大的对象内,某种索引表就可以了.

As you're using anchored search, working on a sorted array could give a much better performance characteristic. Just find the first and last element of your search term with binary search and use that range. Maybe without even copying into a new array. This approach puts some workload upfront (maybe before the user even started typing). If your search data is within larger objects, some sort of index tables would do.

这篇关于如何在 iOS swift 2 中为 uisearchbar 过滤大数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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