RxJava 2.0 和 Kotlin Single.zip() 单选列表
RxJava 2.0 and Kotlin Single.zip() with list of singles
我有无法解决的问题。我正在尝试使用 Kotlin 将 .zip(List, ) 多个 Singles 合并为一个,并且 none 我提供的函数作为第二个参数适合。
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) }
问题在于,由于类型擦除,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 -> }
)
我有无法解决的问题。我正在尝试使用 Kotlin 将 .zip(List, ) 多个 Singles 合并为一个,并且 none 我提供的函数作为第二个参数适合。
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) }
问题在于,由于类型擦除,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 -> }
)