FabianTerhorst / ApiClient

A easy to use api client that combines the power of Retrofit, Realm, Gson, Rxjava and Retrolambda in a easy to use library for Java and Android
Apache License 2.0
96 stars 16 forks source link

ApiClient fetches data on the second attempt only #7

Closed Scit closed 8 years ago

Scit commented 8 years ago

This is the code example (written on Kotlin). I'm trying to fetch jwt token from the server on "get_jwt" button click. On "remove_jwt" I'm deleting all Jwt objects. The problem is that jwtList result is empty on the first click. Then I clicked twice - the data is loaded as need.

        val client = TravelerClient.getInstance<TravelerClient>()
        if (view.id == R.id.get_jwt) {
            client.getJwt("some_access_token").subscribe { jwtList -> if (jwtList.size > 0) input.text = jwtList.first().token }
        } else if (view.id == R.id.remove_jwt) {
            client.storage.executeTransaction { realm -> realm.delete(Jwt::class.java) }
            input.text = ""
        }
Scit commented 8 years ago

it seems you're retrieving data from realm db (which is empty on the first call), after that making a request to a server and finally updating realm (ApiObserver.java), so subscriber don't getting the actual result.