DonDel
DonDel

Reputation: 1

Custom constructor PageKeyedDataSource() crashes app in datasource.factory() of paging library

I'm trying to send a search query to my custom datasource used in the datasource factory of the paging library. When my datasource class is empty or when I have an empty constructor together with a retrofit client that performs a get request without parameters then there is no problem and it loads data just fine. However, when I modify the class to contain a search query for the retrofit client my app crashes when returning the datasource in AudiofileDataSourceFactory.create() as a result of the following call:

val audiofileList : LiveData<PagedList<Audiofile>> =
            dataSourceFactory.toLiveData(pageSize = 50)

in my viewmodel

My datasource factory looks as follows:

class AudiofileDataSourceFactory : DataSource.Factory<Int, Audiofile>(){
    private var searchQuery = ""
    var mutableLiveData = MutableLiveData<AudiofileDataSource>()

    override fun create(): DataSource<Int, Audiofile> {
        val audiofileDataSource = AudiofileDataSource(searchQuery)
        mutableLiveData.postValue(audiofileDataSource)
        return audiofileDataSource
    }

    fun setSearchQuery(query : String){
        searchQuery = query
    }

}

and my data source looks as follows.


class AudiofileDataSource(text : String) : PageKeyedDataSource<Int, Audiofile>() {
    var searchString = text

    override fun loadInitial(params: LoadInitialParams<Int>, callback: LoadInitialCallback<Int, Audiofile>) {

        retrofitProvider.instance.getAudiofiles(searchString).enqueue(object : Callback<List<Audiofile>> {
            override fun onFailure(call: Call<List<Audiofile>>, t: Throwable) {
                print(t.message)
            }
            override fun onResponse(call: Call<List<Audiofile>>, response: Response<List<Audiofile>>) {
                val audiofileList = response.body()!!
                callback.onResult(audiofileList, null, 2)
            }
        })
    }

    override fun loadAfter(params: LoadParams<Int>, callback: LoadCallback<Int, Audiofile>) {
        retrofitProvider.instance.getAudiofiles(searchString).enqueue(object : Callback<List<Audiofile>> {
            override fun onFailure(call: Call<List<Audiofile>>, t: Throwable) {
            }
            override fun onResponse(call: Call<List<Audiofile>>, response: Response<List<Audiofile>>) {
                val audiofileList = response.body()!!
                callback.onResult(audiofileList, params.key + 1)
            }
        })
    }

    override fun loadBefore(params: LoadParams<Int>, callback: LoadCallback<Int, Audiofile>) {

    }
}

If in the data source I remove the searchquery and pass no parameters to the retrofit call then it works fine. But as it is now it crashes upon return audiofileDataSource in the AudiofileDataSourceFactory.create() method. I was thinking that perhaps the return type cannot be converted to a DataSource<Int, Audiofile> but I am not sure why.

Upvotes: 0

Views: 279

Answers (1)

DonDel
DonDel

Reputation: 1

Solved:

The code above works fine but retrofit threw an exception (due to a malformed request) which I didn't catch and thus my program crashed.

Upvotes: 0

Related Questions