I was using live data with paging library 2 but no...
# android
s
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
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
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
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
@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
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...