【发布时间】:2018-08-28 17:36:40
【问题描述】:
上下文:我正在从我的后端请求大量 JSON 数据,其中包括 VPS 中不断更新的 JSON。
我请求我的第一个 JSON 文件,其中包含我的电影发行日期和电影 ID;示例:
[
{
"id": 101,
"release_date": "2018-03-23",
}, {...}
]
我请求的第二个 JSON 文件包含电影的一般信息,例如名称和描述,示例;
[
{
"id": 101,
"name": "PACIFIC RIM UPRISING",
"description": "Here's a cool description",
"genres:": [12, 11, 8]
}, {...}
]
请求的两个文件都保存在用户的手机中。
现在开始解析(这个问题的主题)。我首先在 ArrayList 中解析发布 JSON(Release 是一个包含电影 ID 和发布日期的对象),然后我在 HashMap 中解析电影信息 JSON(整数是电影的 id,Movie 是包含名称的对象,然后是流派列表和描述)。
现在,当我想按流派过滤发布时(用户可以一次按多个流派过滤 recyclerview/data),我使用以下代码:
Collections.sort(userGenres); // user chosen genres gotten from a shared pref
ArrayList<Release> newReleases = new ArrayList<>(); // the list with only the movie the user wants by genre
for (Release release : mUpcomingReleases) {
Movie movie = gameInfoHashMap.get(release.id); // gameInfoHashMap is the hashmap which contains our movie info (it has the genres list)
ArrayList<Integer> releaseGenres = movie.genres;
Collections.sort(releaseGenres);
if (!releaseGenres.equals(genres)) {
// if they don't contain the same genres chosen by the user, get out
break;
} else {
// add it to the list
newReleases.add(release);
}
}
现在的问题是它似乎根本没有效率。您需要了解我的 mUpcomingReleases 包含像一千个发布对象,有没有更好的方法来按流派过滤?我的应用程序感觉很慢。还是有更好的方法来更改我的应用程序的解析以使其更轻量级?
【问题讨论】:
-
这种过滤最适合在后端完成。一旦用户选择了他们的流派,您应该使用指定的流派向您的后端发出请求。然后你的后端可以通过 JSON 只返回那些类型的电影。