RxJava 2.0 and Kotlin Single.zip() with list of singles
Asked Answered
A

1

10

I have issue that I cannot solve. Im trying to .zip(List, ) multiple Singles into one using Kotlin and none of Functions i supply as second argument fits.

    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

Aspectual answered 6/1, 2018 at 16:43 Comment(2)
The function's Java definition is Function<? super Object[], R> so you need to express Object[] in Kotlin.Abrahan
I'd try writing this file in Java, without lambdas, then convert it over to Kotlin with Convert Java to KotlinAwhile
F
7

The problem is that, because of type erasure, the type of the parameters to the zipper function are unknown. As you can see in the definition of zip:

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

You'll have to use Any as the input of your array, and cast to whatever you need each of them to be:

roomDatabase.userFriendsDao().getUserFriendsDtosForToken(token).subscribe(
        { userFriends: List<UserFriendDTO> ->
            Single.zip(
                    getLocationSingleForEveryUser(userFriends),
                    Function<Array<Any>, List<Location>> { t: Array<Any> -> listOf<Location>() })
        },
        { error: Throwable -> }
)
Foison answered 13/9, 2018 at 18:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.