

Once the response comes back from the server, choresChanged PublishProcessor emits a new empty ( Unit) item. What happens is that when addChore is invoked, POST chores http endpoint is called. To avoid nested Flowables, flattening is required ( flatmap = map + flatten) We do that by calling getChores endpoint, which also returns Flowable. var chores = listOf() //no need to provide type on left side of statementīackend.addChore(chore).enqueue(object : Callback - after choresChanged emits an empty item, we need to map it into chores. Using shiny, new interface it's possible to add chores and fetch them from a server. No equals, hashcode, constructors or toString. In Kotlin, DTOs can be defined as immutable data classes: data class ChoreId(val choreId: Long)ĭata class GetChoreDto(val id: Long,val name: String, val points: Int,val interval: Int?)ĭata class GetChoresDto(val chores: List)ĭata class AddChoreDto(val name: String, val points: Int, val interval: Int?) Using retrofit, we can define http endpoints in a type safe way: interface BackendService getChores() : addChoreDto: AddChoreDto) : Call The very first step was adding and getting chores from the server. The Process Consuming REST api with Retrofit I've used Retrofit before and had very positive experience with it.Īpart from that, it integrates nicely with RxJava. This is a type safe http client, handling all serialization and deserialization into user defined classes behind the scenes.


It provides a way to chain and combine asynchronous flows of data. It usually leads to callback hell and complex state management. Traditionally it is done by listening to user input/IO and then changing state as a reaction to these events. We still need a way to glue these sources with the app. UI needs to be responsive at all times - that's why they need to be handled on a different threads. RxJava 2Īlmost every mobile app has at least two asynchronous sources of data - user input and IO (network/storage). Kotlin surprised me also with its great compile time (almost matching Java). It is already stable and has an excellent IDE support. Kotlin is JVM language which makes it compatible with existing Java libraries (RxJava and Retrofit being some of them).Īpart from that Kotlin is more functional than Java which helps to deal with the approach imposed by RxJava. The remaining decision was language and stack for it.
USE RXJAVA WITH RETROFIT ENQUEUE ANDROID
I am a backend developer and my go-to language is Scala, so the choice for server side was quite clear.įor client side I decided to go with Android since doing house chores is highly mobile activity.Ĭonsidering nature of house chores, mobile client seemed like a good idea. The idea was to define a set of chores and assign them to users periodically.Įvery night, chores for the upcoming day are distributed to users (based on what they have done in the past).

but my variable gets null at first and after seconds, when retrofit completed its task, my variable gets data but log value doesn't update.Recently I decided to create a pet project which would help me and my girlfriend split house chores. I have a button in my view and when I click on it, the request method gets to start and subscription write a log of its own data. I config retrofit, and also use observable and subscribe in ViewModel to make observable variable to use in activity binding layout. I'm new in kotlin and I'm trying to use retrofit with Rxjava and live data in MVVM architecture.
