代码之家  ›  专栏  ›  技术社区  ›  vihkat

rxjava2迭代列表并为每个项调用单个

  •  0
  • vihkat  · 技术社区  · 6 年前

    我有一个带朋友连接的数据库表:

    下一个示例只是一个 想象中的 示例,因此请不要建议我以另一种方式实现它。

    友情连接:

    • 人性化

    我有一张带这些身份证的清单。

    val friendConnections=arraylistof(
    friendConnection(id=10,humanid=141)
    FriendConnection(ID=13,HumanID=142)
    )
    

    我想用rxjava制作这个结果列表。

    val friendlist=arraylistof(
    friend(friendConnectionID=10,humanID=141,humanInstance=…),
    朋友(FriendConnectionID=13,HumanID=142,HumanInstance=…)
    )
    < /代码> 
    
    

    我怎么能找到人?

    fun gethumanbyid(id:long):single<human>
    < /代码> 
    
    

    因此,我需要迭代friendConnections并为每个人进行一次调用。我需要将新的人类实例的friendConnection映射到friend实例。

    我试过了,但不管用:

    observable.fromiterable(arraylistof(
    friendConnection(id=10,humanid=141)
    FriendConnection(ID=13,HumanID=142)
    )
    .flatmapsingle friendConnection->
    repository.gethumanbyid(friendconnection.humanid)
    是吗?.map人类->
    朋友(friendconnection.id,friendconnection.human id,human)
    }
    }
    ()
    .subscribeon(schedulers.io())
    .observeon(AndroidSchedulers.mainthread())
    。订阅(IT->
    timber.i(“朋友名单在这里:$it”)。
    },{
    扔掉它
    })
    < /代码> 
    
    

    有人知道怎么了?(我想用RX实现,而不是用房间)

    .

    友情连接:

    • 身份证件
    • 拟人

    我有一张带这些身份证的清单。

    val friendConnections = arrayListOf(
        FriendConnection(id=10, humanId=141),
        FriendConnection(id=13, humanId=142)
    )
    

    我想用RXJava制作这个结果列表。

    val friendList = arrayListOf(
        Friend(friendConnectionId = 10, humanId = 141, humanInstance = (...)),
        Friend(friendConnectionId = 13, humanId = 142, humanInstance = (...))
    )
    

    我怎么能找到人?

    fun getHumanById(id: Long) : Single<Human>
    

    所以我需要重复friendConnections为每个人打一个电话。我需要将新的人类实例的friendConnection映射到friend实例。

    我试过了,但没用:

    Observable.fromIterable(arrayListOf(
                    FriendConnection(id=10, humanId=141),
                    FriendConnection(id=13, humanId=142)
            ))
                    .flatMapSingle { friendConnection ->
                        repository.getHumanById(friendConnection.humanId)
                                ?.map {human ->
                                    Friend(friendConnection.id,friendConnection.humanId,human)
                                }
                    }
                    .toList()
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe({ it ->
                        Timber.i("friendList is here: $it")
                    }, {
                        throw it 
                    })
    

    有人知道怎么了?(我想用RX实现,而不是用房间)

    2 回复  |  直到 6 年前
        1
  •  0
  •   nyarian    6 年前

    这个解决方案适合你吗?

    Observable.fromIterable(
            listOf(
                FriendConnection(1, 101),
                FriendConnection(2, 102)
            )
        )
            .flatMapSingle { connection ->
                getHumanById(connection.humanId)
                    .map { human -> Friend(connection.id, connection.humanId, human) }
            }
            .toList()
            .subscribe { list -> println(list) } //method reference generates error due to methods ambiguity
    

    按ID获取人的模拟函数:

    private fun getHumanById(id: Long): Single<Human> = Single.just(Human(id, id.toString()))
    
        2
  •  0
  •   Suryavel TR Paul Boddington    6 年前

    你可以这样做,

    Observable.fromIterable(friendConnections)
            .flatMap { Observable.just(Friend(it.id, it.humanId, repository.getHumanById(it.humanId).blockingGet())) }
            .toList()
            .subscribe {it --> /*Your Operation*/