1

I was looking other questions but this one is using a different method.

I’m using MVVM in my Android app.

Actually this is the way that I’m getting data from my server:

  • Inject dataManager into viewModel.
  • viewModel calls dataManager->fetchUsers
  • fetchUsers make request to server and return an Observable of Several which in this case should be Several but it’s generic.
  • viewModel subscribe to this request and expect a Several.
  • At this point everything works besides Several doesn’t have a list of User. This several have a list of LinkedTreeMap

enter image description here

I tried to change my dataManager to return a string then map the response in my viewModel but the thing with this is that I will have to do that in every request.

Also I tried to map the request in my dataManager but I got the same link tree map array.

The thing with TypeToken approach is that I have to map in my viewModel.

UPDATED

UsersViewModel

 private fun fetchUsers() {
        isLoading.set(false)
        compositeDisposable += dataManager.GET<User>(classJava = User::class)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe( { response ->
                    isLoading.set(false)
                   // Here the response should be a several with a list of users.
                }, { err ->
                    isLoading.set(false)
                })
    }

GET function:

 override fun <T> GET(classJava: KClass<*>): Single<Several<T>> {

    return Rx2AndroidNetworking.get("EndPoint")
            .addHeaders("Authorization",  apiHeader.protectedAPIHeader.accessToken!!)
            .setOkHttpClient(APIClient.getUnsafeClient())
            .build()
             // Also I tested with
             // .getObjectSingle(Several::class.java) // The thing is that I can't assign the type <T> with this approach.
            .stringSingle
            .map {

                val fromJSON = fromJson<Several<T>>(it)
                fromJSON

            }
}

fromJson function:

inline fun <reified T> fromJson(json: String): T = Gson().fromJson(json, object: TypeToken<T>() {}.type)

Several.kt

data class Several<T>(val items: MutableList<T>)

If I change my GET function to return a String then in UsersViewModel I add a .map like this

private fun fetchUsers() {
        isLoading.set(false)
        compositeDisposable += dataManager.GET<User>(classJava = User::class)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .map(
                    val severalUsers = fromJson<Several<User>>(it)
                    severalUsers 

                )
                .subscribe( { response ->
                    isLoading.set(false)
                   // usersLiveData.value = response
                }, { err ->
                    Log.e("tag", "Ocurred some error")
                    isLoading.set(false)
                })
    }

Then I would have a list of Users as expected but What I dont want to do is map the response in UsersViewModel because I would have to do in every single request that expect a list of items.

Is there any way that I could get a several object without map in my viewmodel?

Dex Sebas
  • 247
  • 3
  • 13

0 Answers0