https://kotlinlang.org logo
#android
Title
# android
s

Striker

06/18/2020, 8:51 AM
I was using live data with paging library 2 but now i upgraded to paging 3..I have a listData(which is a flow that returns paging data) and a searchQuery(livedata) and filterBy(livedata) so whenever i change it i also need the pagingdata to be updated...should i use MediatorLiveData ? Or is there any method using slow for this ?
o

okarm

06/19/2020, 8:26 PM
Transformations.switchMap
will be your friend here. https://developer.android.com/reference/androidx/lifecycle/Transformations
I'll give you a hint from my codebase
Copy code
private val selection = MutableLiveData<DateSelection>(Today)

val pagedReceipts: LiveData<PagingData<ReceiptPostBody>> = selection.switchMap { dateSelection ->
    Pager(config = ...,
        initialKey = ...,
        pagingSourceFactory = { MyPagingSource() }
    ).liveData
}
s

Striker

06/20/2020, 7:03 AM
if it was one then this works...but as i said i have two live data filterBy and searchQuery and whenever any of these change i need to update the list
sorry misspelled flow as slow😅
s

Striker

06/20/2020, 10:39 AM
isn't using flow better than using livedata ? and can we use channel or something for the two livedata(filter,query) ? And whenever this change return the pager as flow ? i couldn't find any similar example for this...
o

okarm

06/20/2020, 12:06 PM
@Striker If you really need or want to use
Flow
instead of
LiveData
you can use the
LiveData.asFlow()
extension function to convert your
filterBy
and
searchQuery
into `Flow`s and then use
combine
and
flatMapConcat
to produce the final
Flow<PagingData>
It would look something like this:
Copy code
val latestPagingData: Flow<PagingData<SomeModel>> = searchQuery.asFlow().combine(filterBy.asFlow()) { query, filter -> Pair(query, filter) }.flatMapConcat { params -> Pager(...).flow }
👍 1
s

Striker

06/20/2020, 12:53 PM
Thanks...is there any problem with this method ? and could you give any reference to these operators(documentation,samples etc...)as i dont understand flatMapConcat etc...
9 Views