我正在尝试使用Fountain(https://github.com/xmartlabs/fountain)来帮助我处理分页和本地/远程数据源。
我正在这样调用库的内部函数:
FountainRx.createNetworkWithCacheSupportListing(
networkDataSourceAdapter = networkDataSourceAdapter,
cachedDataSourceAdapter = cachedDataSourceAdapter,
firstPage = FountainConstants.DEFAULT_FIRST_PAGE,
ioServiceScheduler = Schedulers.io(),
ioDatabaseScheduler = Schedulers.io(),
pagedListConfig = FountainConstants.DEFAULT_PAGED_LIST_CONFIG)
但是我得到的只是这个编译器错误:
Type inference failed: Cannot infer type parameter NetworkValue in fun <NetworkValue, DataSourceValue> createNetworkWithCacheSupportListing(networkDataSourceAdapter: RxNetworkDataSourceAdapter<out ListResponse<out NetworkValue>>, cachedDataSourceAdapter: CachedDataSourceAdapter<NetworkValue, DataSourceValue>, ioServiceScheduler: Scheduler = ..., ioDatabaseScheduler: Scheduler = ..., firstPage: Int = ..., pagedListConfig: PagedList.Config = ...): Listing<DataSourceValue>
None of the following substitutions
(RxNetworkDataSourceAdapter<out ListResponse<out BeerListResponse<List<Beer>>>>,CachedDataSourceAdapter<BeerListResponse<List<Beer>> Any?>,Scheduler,Scheduler,Int,PagedList.Config)
(RxNetworkDataSourceAdapter<out ListResponse<out List<Beer>>>,CachedDataSourceAdapter<List<Beer>, Any?>,Scheduler,Scheduler,Int,PagedList.Config)
can be applied to
(RxNetworkDataSourceAdapter<BeerListResponse<List<Beer>>>,CachedDataSourceAdapter<BeerListResponse<List<Beer>>, Beer>,Int,Scheduler,Scheduler,PagedList.Config))
有什么问题的想法吗?
下面您可以找到创建RxNetworkDataSourceAdapter的代码:
private fun createNetworkDataSourceAdapter(): RxNetworkDataSourceAdapter<BeerListResponse<List<Beer>>> {
val pageFetcher = object : RxPageFetcher<BeerListResponse<List<Beer>>> {
override fun fetchPage(page: Int, pageSize: Int): Single<BeerListResponse<List<Beer>>> =
remoteSource.getBeers(page = page, pageSize = pageSize)
}
return pageFetcher.toTotalEntityCountNetworkDataSourceAdapter()
}
如果您需要更多信息,请告诉我! 在此先感谢:)
答案 0 :(得分:0)
我解决了!就像编译器说的那样,是类型推断问题。
使用以下代码:
override fun getAllBeers(): Listing<Beer> {
val networkDataSourceAdapter: RxNetworkDataSourceAdapter<out BeerListResponse<out List<Beer>>> = createNetworkDataSourceAdapter()
val cachedDataSourceAdapter: CachedDataSourceAdapter<List<Beer>,Beer> = getBeerLocalSource()
return FountainRx.createNetworkWithCacheSupportListing(
networkDataSourceAdapter = networkDataSourceAdapter,
cachedDataSourceAdapter = cachedDataSourceAdapter,
firstPage = FountainConstants.DEFAULT_FIRST_PAGE,
ioServiceScheduler = Schedulers.io(),
ioDatabaseScheduler = Schedulers.io(),
pagedListConfig = FountainConstants.DEFAULT_PAGED_LIST_CONFIG)
}
使用out kotlin关键字来显式类型,错误消息终于消失了。
此Kotlin文档页面中的更多信息:https://kotlinlang.org/docs/reference/generics.html