首页 文章

RxJava 2.0和Kotlin Single.zip()以及单打列表

提问于
浏览
3

我有一个问题,我无法解决 . 我试图.zip(List,)使用Kotlin将多个Singles合并为一个,而我提供的函数都没有作为第二个参数适合 .

fun getUserFriendsLocationsInBuckets(token: String) {
    roomDatabase.userFriendsDao().getUserFriendsDtosForToken(token).subscribe(
            { userFriends: List<UserFriendDTO> ->
                Single.zip(getLocationSingleForEveryUser(userFriends),
                        Function<Array<List<Location>>, List<Location>> { t: Array<List<Location>> -> listOf<Location>() })
            },
            { error: Throwable -> }
    )
}

private fun getLocationSingleForEveryUser(userFriends: List<UserFriendDTO>): List<Single<List<Location>>> =
        userFriends.map { serverRepository.locationEndpoint.getBucketedUserLocationsInLast24H(it.userFriendId) }

Android studio error

1 回答

  • 1

    问题是,由于类型擦除, zipper 函数的参数类型是未知的 . 正如您在 zip 的定义中所看到的:

    public static <T, R> Single<R> zip(final Iterable<? extends SingleSource<? extends T>> sources, Function<? super Object[], ? extends R> zipper)
    

    你必须使用 Any 作为你的数组的输入,并转换为你需要的任何东西:

    roomDatabase.userFriendsDao().getUserFriendsDtosForToken(token).subscribe(
            { userFriends: List<UserFriendDTO> ->
                Single.zip(
                        getLocationSingleForEveryUser(userFriends),
                        Function<Array<Any>, List<Location>> { t: Array<Any> -> listOf<Location>() })
            },
            { error: Throwable -> }
    )
    

相关问题