Can we set Flowable
to emit every item when is ready, but append it to list (something like buffer(int size)
but without clear function)
I make a search query on a database that returns a Flowable <List <ItemEntity>>
, then I map each ItemEntity
to an ItemDomain
, and it returns a Flowable <List <ItemDomain>>
.
Is it possible for a Flowable <List <ItemDomain>>
to return values sequentially, without waiting for the whole result(i.e. a one-item list first, then a two-item list, then a three-item list, etc.).
When I change my search query, I want to receive the new list again.
override fun observeGroupsBySearchQuery(query: String): Flowable<List<Group>> {
return groupsDao.observeGroupsBySearchQuery(query)
.switchMap {
Flowable.fromIterable(it)
.map { group ->
Group(group.symbol, group.number, group.name)
}
.toList() // I dont want to wait until finish, but I dont want to loose earlier emitted items
.toFlowable()
}
}
@Query(
value = """
SELECT *
FROM Groups
WHERE symbol || number || name LIKE '%' || :query || '%' AND number != -1 AND active IS 1
""")
abstract fun observeGroupsBySearchQuery(query: String): Flowable<List<GroupsEntity>>
And other interesting question is whether the database (room) can return the results one by one (do we have to use pagination
for this)?
Sounds like you need scan
to receive intermediate aggregates:
groupsDao.observeGroupsBySearchQuery(query)
.switchMap(it ->
Flowable.fromIterable(it)
.map(group -> Group(group.symbol, group.number, group.name))
.scan(new ArrayList<>(), (state, t) -> { state.add(t); return state; })
.skip(1)
)
If you plan to consume the intermediate list on a different thread, you'll have to make a copy every time:
.skip(1)
.map(list -> new ArrayList<>(list))