Kotlin Collections with RxJava zip

Kollections (Collections aka in Kotlin) are very similar to collections in Java. We do use them on a very regular basis and they are List, Set or Map which are majorly used in Kotlin. They can be either Mutable or Immutable.

Purpose of this article is to help understand how can we leverage collections with RxJava Zip. In order to understand this I will use a REST API example with client making a request by selecting multiple items from same or different group and how it handles the response using RxJava.

I currently use Single for the responses to flow and update database. In order to understand why I use Single vs MayBe or vs Observable this article explains it really well. https://medium.com/android-news/rxjava-single-maybe-and-completable-8686db42bac8

Lets get started. Imagine you have a list of items and these items are grouped depending on its unique primary ID.

Lets say the structure for model name Item and ItemGroup looks like below:

data class Item {   var itemID: Int,
var itemDescription: String,
var itemGroupID: Int
}data class ItemGroup { var groupID: Int,
var groupDescription: String
}

When I make a request (using Square Retrofit library https://square.github.io/retrofit/) to get Items for a particular id, I get the following response in JSON.

[
{
id: 1,
description: paper,
groupid: 4
},
{
id: 2,
description: pen,
groupid: 1
},
{
id: 3,
description: paper,
groupid: 4
}
{
id: 4,
description: desk,
groupid: 2
}
]

So the idea is to move multiple items from same group or multiple groups to another group. After selecting multiple items and move items from one group to other following is the code snippet I have used to make one request. Also I want to make only a single request to make sure if there are errors in lets say one of the requests; I really dont have to loop through errors to find which request failed. The zip functionality in RxJava helps me in achieving that where I can combine multiple requests in one and it emits success if all the request in the list is a success or failure if any one of them fails.

var zipRequest = ArrayList<Single<Response>>var eachSelectedItemIds : MutableList<Item> = mutableListOf()var groupItems = eachSelectedItemIds.groupBy{ it.groupID }// loop through the map<Int, List<Item>>
groupItems.forEach { eachItem ->
var request = Request(
fromID = eachItem.key,
toID = group.groupID,
items = eachItem.value.map { item -> Item(item.itemID) })
zipRequest.add(repository.moveItems(request))// this is an asynchronous request done using Retrofit.
}
// Use Single zip to zip all the requests
Single.zip(zipRequest) { Any()
}. subscribe(
{
Log.e("Success")
}, {
Log.e("Failure")
}
)

I had to use zip because of the limitations on the backend where they could not create a structure which have multiple items as a list of same group. So as you see from above code fromID is just an Integer and not an array of integers belonging to different groups.

RxJava zip helps in combining many requests into one and will deliver only one response. In this way we can achieve simple functionality but a very important one where we run multiple tasks parallely and return only a single callback when all the tasks are done.

Please let me know if you guys liked my first article by adding claps and do let me know if it was helpful by providing feedback in the below comments section.

Experienced Mobile Developer focusing on Android and iOS.