首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >我正在尝试将地图嵌套到RxJava2中的过滤器中,这样我就可以删除所有没有正确平台的对象。但是不能建造它

我正在尝试将地图嵌套到RxJava2中的过滤器中,这样我就可以删除所有没有正确平台的对象。但是不能建造它
EN

Stack Overflow用户
提问于 2020-02-19 23:02:46
回答 2查看 70关注 0票数 0
代码语言:javascript
复制
override fun getVideoGamesRepository(platformName: String) {
    gameRepo = VideoGameRepositoryImpl()
    compositeDisposable.add(gameRepo.returnVideoGames()
        .filter { gameModel -> gameModel.gamesResults.map {  } }
        .subscribe())
}

下面是我从api中过滤数据的方法。我将只包含相关的数据片段。

我正在尝试将数据库中平台名称的值与我在上一个活动中输入的平台名称进行比较。但在过滤器内部的.map处,.map带红色下划线。

代码语言:javascript
复制
{
"count": 363773,
"next": "https://api.rawg.io/api/games?page=3",
"previous": "https://api.rawg.io/api/games",
"results": [
    {
        "id": 28,
        "slug": "red-dead-redemption-2",
        "name": "Red Dead Redemption 2",
        "released": "2018-10-26",
        "tba": false,
        "background_image": "https://media.rawg.io/media/games/511/5118aff5091cb3efec399c808f8c598f.jpg",
        "rating": 4.54,
        "rating_top": 5,
        "ratings": [
            {
                "id": 5,
                "title": "exceptional",
                "count": 1466,
                "percent": 70.28
            },
            {
                "id": 4,
                "title": "recommended",
                "count": 409,
                "percent": 19.61
            },
            {
                "id": 3,
                "title": "meh",
                "count": 143,
                "percent": 6.86
            },
            {
                "id": 1,
                "title": "skip",
                "count": 68,
                "percent": 3.26
            }
        ],
        "ratings_count": 2056,
        "reviews_text_count": 25,
        "added": 6296,
        "added_by_status": {
            "yet": 284,
            "owned": 3522,
            "beaten": 912,
            "toplay": 951,
            "dropped": 161,
            "playing": 466
        },
        "metacritic": null,
        "playtime": 22,
        "suggestions_count": 630,
        "user_game": null,
        "reviews_count": 2086,
        "saturated_color": "0f0f0f",
        "dominant_color": "0f0f0f",
        "platforms": [
            {
                "platform": {
                    "id": 4,
                    "name": "PC",
                    "slug": "pc",
                    "image": null,
                    "year_end": null,
                    "year_start": null,
                    "games_count": 203916,
                    "image_background": "https://media.rawg.io/media/games/8e0/8e032ac4faf1136e7d708bb3ac61af23.jpg"
                },
                "released_at": "2018-10-26",
                "requirements_en": null,
                "requirements_ru": null
            },
            {
                "platform": {
                    "id": 1,
                    "name": "Xbox One",
                    "slug": "xbox-one",
                    "image": null,
                    "year_end": null,
                    "year_start": null,
                    "games_count": 3085,
                    "image_background": "https://media.rawg.io/media/games/c24/c24ec439abf4a2e92f3429dfa83f7f94.jpg"
                },
                "released_at": "2018-10-26",
                "requirements_en": null,
                "requirements_ru": null
            },
            {
                "platform": {
                    "id": 18,
                    "name": "PlayStation 4",
                    "slug": "playstation4",
                    "image": null,
                    "year_end": null,
                    "year_start": null,
                    "games_count": 4504,
                    "image_background": "https://media.rawg.io/media/games/4be/4be6a6ad0364751a96229c56bf69be59.jpg"
                },
                "released_at": "2018-10-26",
                "requirements_en": null,
                "requirements_ru": null
            }
        ],
EN

回答 2

Stack Overflow用户

发布于 2020-02-19 23:17:51

filter方法以predicate作为参数。因此您必须为筛选器定义一个返回true或false的条件。

根据你的模型,下面的代码应该返回包含你想要的平台的游戏结果。

代码语言:javascript
复制
compositeDisposable.add(gameRepo.returnVideoGames()
  .map { gameModel ->
    gameModel.gamesResults.filter { gameResult -> gameResult.platforms.any { platform -> platform.name == platformName } }
  }
  .subscribe())

而且,您似乎将整个响应对象存储在您的数据库中。但我认为这是不必要的,也不太实际。相反,您可以存储游戏结果对象。从而让你减少操作次数,达到同样的效果。例如:

代码语言:javascript
复制
compositeDisposable.add(gameRepo.returnVideoGames()
  .filter { gameResult -> gameResult.platforms.any { platform -> platform.name == platformName } }
  .subscribe())
票数 0
EN

Stack Overflow用户

发布于 2020-02-20 23:55:17

我是个笨蛋,我所需要做的就是以不同的方式查询api调用。很抱歉,伙计们。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60303227

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档