PositionalDataSource returns nothing from Parse Server databse

48 Views Asked by At

I'm trying to load Products items from a Parse Server database using data source type of PositionalDataSource()to be able to fetch pages of items while scrolling down.

This what i've done so far :

My PositionalDataSource() class

class ParsePositionalDataSource: PositionalDataSource<Product>() {

    private fun getQuery() : ParseQuery<Product> {
        return ParseQuery.getQuery(Product::class.java).orderByDescending("createdAt")
    }

    override fun loadRange(params: LoadRangeParams, callback: LoadRangeCallback<Product>) {
        val query = getQuery()

        query.limit = params.loadSize
        query.skip = params.startPosition

        val products = query.find()

        callback.onResult(products)
    }

    override fun loadInitial(params: LoadInitialParams, callback: LoadInitialCallback<Product>) {
        val query = getQuery()

        query.limit = params.requestedLoadSize
        query.skip = params.requestedStartPosition

        val count = query.count()
        val products = query.find()

        callback.onResult(products, params.requestedStartPosition, count)
    }
}

And then a data source factory :

class ParseDataSourceFactory : DataSource.Factory<Int, Product>() {

    override fun create(): DataSource<Int, Product> {
        return ParsePositionalDataSource()
    }


}

And finally my ViewModal class

class CategoryTabViewModel() : ViewModel() {

    private var pagedListConfig: PagedList.Config = PagedList.Config.Builder().setEnablePlaceholders(true)
        .setPrefetchDistance(5)
        .setInitialLoadSizeHint(5)
        .setPageSize(5).build()

    private val sourceFactory = ParseDataSourceFactory()

    private val _productList =  LivePagedListBuilder(sourceFactory, pagedListConfig).build()

    val x = _productList

    //val productList: LiveData<PagedList<Product>> = _productList

    fun getProductList(): LiveData<PagedList<Product>> {
        return _productList
    }
}

I've checked if loadinitial() returns data by setting a breakpoint after : val products = query.find()and yes, productscontains a list of Product

But in my ViewModal, _productList it's always null or empty list.

Do I miss something ?

1

There are 1 best solutions below

0
On

I've found the solution by my self while digging in debugger console output.

I saw a line that says :

E/RecyclerView: No layout manager attached; skipping layout

And it works just fine after i added the layoutmanager in my RecyclerView

<androidx.recyclerview.widget.RecyclerView
        ...
        app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
        ... />

And the documentation says :

A LayoutManager is responsible for measuring and positioning item views within a RecyclerView as well as determining the policy for when to recycle item views that are no longer visible to the user. By changing the LayoutManager a RecyclerView can be used to implement a standard vertically scrolling list, a uniform grid, staggered grids, horizontally scrolling collections and more. Several stock layout managers are provided for general use.

And

When writing a RecyclerView.LayoutManager you almost always want to use layout positions whereas when writing an RecyclerView.Adapter, you probably want to use adapter positions.

It seems that RecyclerView.Adapter needs RecyclerView.LayoutManager to be set.