Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 656
Efka Avatar asked Jan 06 '18 16:01

Efka


1 Answers

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 -> }
)
like image 129
marianosimone Avatar answered Sep 19 '22 18:09

marianosimone