在Android的Live搜索Android、Live

2023-09-06 07:01:41 作者:一往情深深几许

我有以下任务做,我需要建立从端点实时搜索。

I have the following task to do, I need to build a live search from an endpoint.

现在,事情是我知道怎么做,但,我不知道是如何做到这一点效率。小应用程序应做到以下几点:

Now, the thing is I know how to do it but, what I don't know, is how to do it efficient. The small app should do the following:

从特定端点Live搜索的 - 超过2000中的对象JSON请求列表当加载,应用程序应该显示一个微调。刷新自动完成列表视图。

我所知道的唯一的解决方案是:

The only solution I know is the following:

我添加菜单操作栏在那里我添加搜索视图。活动里面,我将有一个列表视图(更好,当我想补充的项目有些点击)片段,当东西的用户搜索(例如,绕,​​第一,然后是AR等),然后向服务器发出新的请求将被制成,所有以A将被加载到ListView,从那时起自动完成会做的工作。启动对象

I add a menu to the action bar where I add the search view. Inside the activity I will have a fragment with a list view (better when I want to add some clicking on the items), and when the user searches for something (e.g, "around, "a" first, then "ar", etc) then a new request to the server will be made and all the objects that start with "a" will be loaded to the listview and from then on autocomplete will do its work.

在这样,我需要加载所有包含有从a和过滤器的数据。

In this way I need to load all the data that contain "a" and filter from there.

现在我的问题是,我需要解析每次JSON文件,并有可能我只需要3条记录。

Now the problem for me is that I need to parse every time the json file, and it is possible I only need 3 records.

作为改进,我认为在某种程度上过滤JSON文件大和检索唯一我需要的JSON文件,但我不知道这是可能的,如果是在什么样的代价。

As an improvement, I thought of somehow filtering the json big file and retrieving only the json file that I need, but I am not sure if this is possible and if so at what cost.

有谁知道一个更好的解决方案?

Does anyone know a better solution?

推荐答案

既然你不能改变服务器端组件,你必须让你的客户更聪明。

Since you can't change the server-side component, you'll have to make your client smarter.

去这里的方法是(如 @pskink 建议)在本地存储数据。如果您在搜索视图中的每个输入/删除字符一个请求,你几乎剂量他们的服务器。此外,这将是缓慢的。

The way to go here is (as @pskink suggested) to store the data locally. If you make a single request for every entered/deleted character in the search-view, you're practically DOSing their server. Also, this will be slow.

的挑战是在本地缓存中的数据,但保持新鲜足够,以便它不会从服务器上的数据转移到多。实现这个难度比它的声音。

The challenge is to cache the data locally but keep it "fresh" enough so that it doesn't divert to much from the data on the server. Implementing this is harder than it sounds.

我会建议加载从后端整个JSON 只要打开活动。根据此列表中每个条目有多大,你可能会保持他们都在内存中和过滤上(我在想一个树可能是一个很好的起点)脱身。

I would suggest loading the entire JSON from the Backend as soon as the activity opens. Depending on how big each individual entry in this list is, you might get away with keeping them all in-memory and filtering on that (i'm thinking a Tree might be a good starting point).

如果你得到与此解决方案的任何性能或内存的问题,我建议将数据移动到某种数据库(它不需要是SQLite的,的服务器也可能是有趣)。同样的挑战也适用于那些数据跟上最新的,当然。

If you get any performance or memory problems with this solution, I'd suggest moving the data into some kind of database (it doesn't need to be SQLite, Realm might also be interesting). The same challenge applies for keeping that data up-to-date, of course.

既然你说的 的告诉你,我想你是与服务器组件背后的人的工作。在这种情况下,我发现最好的办法是实现一个有效的解决方案(在任何上述的那些),并展示给他们。如果它不够快他们,提出建议:

Since you said they told you, I guess you're working with the people behind the server-component. In that case, I have found the best approach is to implement a solution that works (any of the ones above) and show it to them. If it's not fast enough for them, make suggestions:

他们可以给你一个API,因为某一特定日期(这样你就可以只更新数据库)来获得只有新/更改的条目。也有可能使混合模式:在您的离线数据第一个过滤器,而且还向服务器(带有过滤器参数或某事)的请求,并混合得到的数据。指示您在获取更多的达最新数据的过程是用户,但他们展示你已经知道了结果。如果项频繁除去这可能无法正常工作。