0

I'm trying to make my DataSource.Factory class, but I get an error when trying to return my PageKeyedDataSource implementation.

class SubredditPageKeyedDataSource(private val service: LedditService,
                               private val subredditName: String): PageKeyedDataSource<String, Children<DataX>>() {

@SuppressLint("CheckResult")
override fun loadInitial(params: LoadInitialParams<String>, callback: LoadInitialCallback<String, Children<DataX>>) {
    service.getSubredditTopics(subredditName).subscribe { result -> callback.onResult(result.data.children, result.data.before, result.data.after) }
}

@SuppressLint("CheckResult")
override fun loadAfter(params: LoadParams<String>, callback: LoadCallback<String, Children<DataX>>) {
    service.getSubredditTopicsAfter(subredditName, after = params.key).subscribe {result -> callback.onResult(result.data.children, result.data.after)}
}

override fun loadBefore(params: LoadParams<String>, callback: LoadCallback<String, Children<DataX>>) {
    // ignored, since we only ever append to our initial load
}

}

Now heres my factory:

class SubredditDataSourceFactory(private val service: LedditService,
                             private val subredditName: String): DataSource.Factory<String, List<Children<DataX>>>() {


override fun create(): DataSource<String, List<Children<DataX>>> {
    return SubredditPageKeyedDataSource(service, subredditName)
    }
}

When returning my DataSource in the above create() method, AndroidStudio gives me an IDE error:

Type mismatch. 
Required: DataSource<String, List<Children<DataX>>>
Found: SubredditPageKeyedDataSource

Am I going insane? PageKeyedDataSource extends from ContiguousDataSource which is a DataSource. Why am I getting a type mismatch? I've checked 2 different examples of the paging library and their implementation is exactly like mine. Unless I missed something???

SpecialSnowflake
  • 945
  • 4
  • 16
  • 32

1 Answers1

0

The types don't actually match.

Your factory is for:

PageKeyedDataSource<String, List<Children<DataX>>>

But your datasource implements the type:

PageKeyedDataSource<String, Children<DataX>>

Notice that one has a list of children on the right side, the other a single child. The one without the List<> should be correct, try removing that part from your code.

Daniel Zolnai
  • 16,487
  • 7
  • 59
  • 71