Paging 3 adapter which needs coroutine data


This Content is from Stack Overflow. Question asked by Daniel sims

I’m using paging 3, with a remote mediator.

My mediator etc all works (tried with hard coded values), but my problem now is that I need to get a users location from a coroutine scope to pass to my adapter.

This is what I’m trying to achieve, but obviously this doesn’t work.

    lateinit var parking: Flow<PagingData<Venue>>

    init {

    fun init() {
        viewModelScope.launch {
            userLocationProvider.getUserLastKnownLocation().result?.let {
                parking = venueRepository.getVenuesByCategory(
                    category = "parking",
                    lat = it.latitude,
                    lon = it.longitude

And then collecting in my composable;

        title = "Parking", venueList = viewModel.parking.collectAsLazyPagingItems()

If I do this

val parking = venueRepository.getVenuesByCategory("parking").cachedIn(viewModelScope)

and hard code my lat/lon this works.

    override fun getVenuesByCategory(
        category: String,
        lat: Double,
        lon: Double
    ) = Pager(
        config = PagingConfig(pageSize = 10),
        remoteMediator = VenueRemoteMediator(
            query = category,
            venueDatabase = venueDatabase,
            venueServices = venueServices,
            lat = lat,
            lon = lon
        pagingSourceFactory = {
                category = category,
                lat = lat,
                lon = lon
    ) { venueDataObjectPagingData ->
        // map data

If I could do userLocationProvider.getUserLastKnownLocation() in my pagingSourceFactory that could also work, but I can’t find a way to use coroutines in there (I could create my own PagingSource, but I’m using the Room Paging Source and can’t find anything about that.


This question is not yet answered, be the first one who answer using the comment. Later the confirmed answer will be published as the solution.

This Question and Answer are collected from stackoverflow and tested by JTuto community, is licensed under the terms of CC BY-SA 2.5. - CC BY-SA 3.0. - CC BY-SA 4.0.

people found this article helpful. What about you?