RxJava Kotlin how to get separated objects from single observable<String> - android

RxJava Kotlin flatmap don't return separated objects from splitted string. instead it returns List
val source: Observable<String> = Observable.just("521934/2342/FOXTROT")
.flatMap{Observable.fromArray(it.split("/"))}
.subscribe{Log.d(TAG, "$it")}
It returns list:
[521934, 2342, FOXTROT]
But book (Thomas Nield : Learning RxJava / 2017 / Page 114) says it has to return separated strings
521934
2342
FOXTROT
example from book
http://reactivex.io/documentation/operators/flatmap.html says that it returns Single object. In my case I got Single List object. So, documentation says true. But I want to get result as in book example!
How I can split the list and get separated objects?

Make use of flatMapIterable, so you can get a stream of the items from the list:
Observable.just("521934/2342/FOXTROT")
.flatMap { input -> Observable.fromArray(input.split("/")) }
.flatMapIterable { items -> items }
.subscribe { item -> Log.d(TAG, item) }

Just use fromIterable:
Observable.just("521934/2342/FOXTROT")
.flatMap { Observable.fromIterable(it.split("/")) }
.subscribe{
Log.d(TAG, "$it")
}
In case of array you would have to use a spread operator additionaly since fromArray takes a vararg argument list:
Observable.fromArray(*arrayOf("521934","2342","FOXTROT"))

Related

How can I observe a table in Room and get entities from other tables based on updates?

I want to use Coroutine flows to achieve this.
I have a DAO/Table in Room: info. This info-table is a lookup that holds id and type for other models. There's a table per type [A, B, C] and the id (PrimaryKey) in the info-table and corresponding type-table are the same.
If I declare a getAll query in the info DAO, I can observe it in a ViewModel and any updates to the table would propagate to the collector
#Query("SELECT * FROM $INFO_TABLE WHERE")
fun getAll(): Flow<List<Info>>
In my "database-class" that have access to all dao I do:
fun getModels(): Flow<List<Model>> {
return infoDao.getAll()
.onEach { Timber.d("Info (${it.size})") }
.flatMapConcat { list ->
val flows = list.map { getModel(it.id, it.type) }
combine(flows) {
Timber.d("Combine (${it.size})")
it.toList()
}
}
The getModel(id, type) returns a Flow from that types dao.
Expected / wanted behaviour:
When a info-model is inserted into the info-dao, the getAll().onEach{ } is called
When the info table gets updated, the Flow<List> emits a new (updated) list.
Actual behaviour:
The onEach for the InfoDao is only ever called once, even when the info table is modified.
The log in combine is called when info is updated, but with the same it.size
What's wrong, how do I fix this?
How do I chain Flows in this manner?
How can I get the getAll() flow to be "re-triggered"?
I managed to get something working, but I would love to hear what's going on and the theory behind it.
(Also, I thought of having a solution with one Flow from the info-table, and simple suspend functions from the model-table but it felt less appealing)
override fun getAllModel(): Flow<List<Model>> = infoDao.getAll()
.onEach { Timber.d("Info (${it.size})") }
.map { list -> list.map { info -> getModel(info.id, info.type) } }
.map { flows -> combine(flows) { it.toList() } }
.flattenMerge()
It seems that the flattenMerge() is the key to this. If I use the default concurrency parameter (16), or a value larger than one (2 works) then I get updates from the info get all. But if the concurrency is set to 1, then it's effectively the same as flattenConcat - which doesn't work!

Kotlin - combining flows

In my viewModel I have:
private val setEntitiesList = mutableStateListOf<Exercise>()
val exercisesFromDB = exerciseDao.getAllExercisesWithSetNo(trainingId)
val exercises =
exercisesFromDB.combine(setEntitiesList.asFlow()) { exercises, setEntitiesList ->
Pair(exercises, setEntitiesList)
}.mapLatest { (exercises, setEntitiesList) ->
//I am altering exercises list here
exercises
}
In my fragment I have:
lifecycleScope.launchWhenStarted {
repeatOnLifecycle(Lifecycle.State.STARTED){
addTrainingViewModel.exercises.collectLatest {
exercisesAdapter.submitList(it)
}
}
}
It doesnt work. Nothing is collected in fragment
If I change in fragment to collect "addTrainingViewModel.exercisesFromDB" it works - values are emited and collected.
What I would like to achieve: new list of exercises is emitted when setEntitiesList or exercisesFromDB are changed and I am able to do sth with a list of exercises before it is emitted
I assume that setEntitiesList variable is of type Iterable. In that case when you convert it to Flow like setEntitiesList.asFlow() you create a cold Flow, it means that when you add new elements to the setEntitiesList iterable they won't be emitted to the combined Flow.
On the other hand, by calling addTrainingViewModel.exercisesFromDB you get a hot Flow, so when you tested it separately you got the right result.
So you can't use a converted iterable object setEntitiesList as hot Flow. You need somehow to rewrite your logic and use a hot Flow instead of cold Flow. You can use MutableSharedFlow for that. The code will be something like the following:
private val setEntitiesListFlow = MutableSharedFlow<Exercise>(extraBufferCapacity = 64)
val exercisesFromDB = exerciseDao.getAllExercisesWithSetNo(trainingId)
val exercises =
exercisesFromDB.combine(setEntitiesListFlow) { exercises, setEntitiesList ->
Pair(exercises, setEntitiesList)
}.mapLatest { (exercises, setEntitiesList) ->
//I am altering exercises list here
exercises
}
// somewhere else in your code emitting Exercise:
setEntitiesListFlow.tryEmit(Exercise(...))

Populate some list before returning it with RX

I'm trying to populate a list with detailed info about an airport. First off, I'm getting the list of airports that fulfill certain condition, and getting detailed info of every item that's in the list. And finally, i'm returning the populated list.
Here'w what i have:
override fun createObservable(params: String): Flowable<List<AirportsEntity>> {
val destinationAirports = mutableSetOf<AirportsEntity>()
return this.searchFlightRepository.getDestinationsByCode(params)
.flatMap {
Flowable.fromIterable(it)
}
.flatMap {
this.searchFlightRepository.getAirportByCode(it.destination)
}
.flatMap {
destinationAirports.add(it)
Flowable.just(destinationAirports.toList())
}
}
The above code works just fine, but it's emitting an observable per item inside the list. I wonder how to change it in order to populate the list first, and then return it when the fetching process is done.
Thanks in advance.
Is the use of Flowable necessary?
Something like this would probably be more suitable:
private val destinations = listOf("1", "2", "3", "4")
fun getAirportDestinations(airportCode: String): Single<List<String>> =
Observable.just(destinations)
.flatMapIterable { it }
.flatMapSingle { getAirportByCode(it) }
.toList()
private fun getAirportByCode(destinationCode: String): Single<String> =
Single.just("ABC1")
"it's emitting an observable per item inside the list" - flatmap will emit for every item. The use of toList() will mean that it "Returns a Single that emits a single item, a list composed of all the items emitted by the finite source ObservableSource."

Combine the latest values of a nested Observable on demand

I have some custom fields that each have a value that can be retrieved using a BehaviorSubject<Value>. What fields are shown are based on what I get from the API, so in the end I have n amount of BehaviorSubject<Value>s. I would like to group these values together into an Observable<List<Value>> where the list contains latest values from these fields (order is irrelevant). The problem however is that these fields are not all available at the same time because they're created while the UI loads so I cannot use Observable.combineLatest with the list of subjects.
What I currently have done is I have created the following variable:
private val values = BehaviorSubject.create<Pair<Int, Value>>()
I use this subject to subscribe to all of the field's subjects but map the subjects first with their position and make a pair of it.
fieldSubject.map {
Pair(position, value)
}.subscribe(values)
What I then want to do is group the values based on their position in the pair and get an Observable<List<Value>> where the list contains the latest values from each position. However I don't know how to proceed after grouping them using groupBy:
values.groupBy {
it.first
}
This results in an Observable<GroupedObservable<Pair, Value>>>. Finally this is how I think I should get to Observable<List<Value>>, but I don't know what to do from here.
groupBy doesn't seem helpful to me here.
Accumulating values is generally done using scan, here you can use a transformation like:
values.scanWith({ arrayOfNulls<Value>(N) }) { acc, (index, value) ->
acc.copyOf().apply {
set(index, value)
}
}
.map { it.filterNotNull() }
You could probably get away without copyOf(), but I think I encountered problems in the past when the accumulator function wasn't pure.
By the way, you can write position to value instead of Pair(position, value).
Also, you can use merge to get an Observable<Pair<Int, Value>> instead of creating a BehaviorSubject and subscribing it manually to all fields:
Observable.mergeArray(
fieldX.map { 0 to it },
fieldY.map { 1 to it },
fieldZ.map { 2 to it }
// ...
)
So overall, you could have a function that does it all for you:
inline fun <reified T : Any> accumulateLatest(vararg sources: Observable<out T>): Observable<List<T>> {
return Observable.merge(sources.mapIndexed { index, observable ->
observable.map { index to it }
})
.scanWith({ arrayOfNulls<T>(sources.size) }) { acc, (index, value) ->
acc.copyOf().apply {
set(index, value)
}
}
.map { it.filterNotNull() }
}
And then just call:
accumulateLatest(fieldX, fieldY, fieldZ)
.subscribe {
println("Latest list: $it")
}

How to chain transformations in Android when using live data?

Given the following setup:
I have 2 repositories: Repository A and Repository B both of them return live data.
I have a ViewModel that uses both of these repositories.
I want to extract something from Repository A and depending on the result I want to grab something from Repository B and then transform the result before returning to UI.
For this I have been looking at the LiveData Transformation classes. The examples show a single transformation of the result however I want something along the lines of chaining two transformations. How can I accomplish this?
I have tried setting something up like this but get a type mismatch on the second transformation block:
internal val launchStatus: LiveData<String> = Transformations
.map(respositoryA.getData(), { data ->
if (data.isValid){
"stringA"
} else {
//This gives a type mismatch for the entire block
Transformations.map(repositoryB.getData(), {
result -> result.toString()
})
}
})
(Also please let me know if there is an alternate/recommended approach for grabbing something for chaining these call i.e. grab something from A and then grab something from B depending on result of A and so on)
Your lambda sometimes returns the String "stringA", and sometimes returns the LiveData<String> given by:
Transformations.map(repositoryB.getData(), {
result -> result.toString()
})
This means that your lambda doesn't make sense - it returns different things in different branches.
As others have mentioned, you could write your own MediatorLiveData instead of using the one given by Transformations. However, I think it's easier to do the following:
internal val launchStatus: LiveData<String> = Transformations
.switchMap(respositoryA.getData(), { data ->
if (data.isValid) {
MutableLiveData().apply { setValue("stringA") }
} else {
Transformations.map(repositoryB.getData(), {
result -> result.toString()
})
}
})
All I've done is made the first code branch also return a LiveData<String>, so now your lambda makes sense - it's a (String) -> LiveData<String>. I had to make one other change: use switchMap instead of map. This is because map takes a lambda (X) -> Y, but switchMap takes a lambda (X) -> LiveData<Y>.
I used MediatorLiveData to solve this problem.
MediatorLiveData can observer other LiveData objects and react to them.
Instead of observing either of the repositories. I created myData (instance of MediatorLiveData) in my ViewModel class and have my view observe this object. Then I add Repository A as the initial source and observe that and only add Repository B if the result of A requires it. This allows me to keep the transformations associated with the live data of each of the repo and still process each result in the correct order. See below for implementation:
internal val myData: MediatorLiveData<String> = MediatorLiveData()
private val repoA: LiveData<String> = Transformations.map(
respositoryA.getData(), { data ->
if (data.isValid) "stringA" else ""
})
private val repoB: LiveData<String> = Transformations.map(
repositoryB.getData(), { data -> "stringB"
})
fun start() {
myData.addSource(repoA, {
if (it == "stringA") {
myData.value = it
} else {
myData.addSource(repoB, {
myData.value = it
})
}
})
}
Note: The solution does not cover the case where repoB might be added multiple times but it should be simple enough to handle.
I would try using switchMap instead of map:
Similar to map(), applies a function to the value stored in the LiveData object and unwraps and dispatches the result downstream. The function passed to switchMap() must return a LiveData object.
You can nest transformations.
val finalLiveData = Transformations.switchMap(liveData1){
val search = it
Transformations.switchMap(liveData2) {
db(context).dao().all(search, it)
}
}
You can transform the data by using switchmap. Here's an example documentation.

Categories

Resources