我遇到了使用Dagger2保存“缓存”数据的问题。
我有Application Component
和Authentication Subcomponent
。
Authentication Subcomponent
具有缓存模块:
@Module
class AuthCacheModule {
@AuthScope
@Provides
fun provideGeoLocation(): GeoInfo = GeoInfo()
@AuthScope
@Provides
fun provideLastKnownLocation(): GeoLocation = GeoLocation()
@AuthScope
@Provides
fun provideCountries(): List<Country> = mutableListOf()
}
前两个缓存对象工作正常。国家列表有问题。每次我调用从网络获取或从高速缓存List<Country>
获取高速缓存的方法时,始终为空。
override fun getCountryList(): Observable<List<Country>> =
Observable.just(countries)
.flatMap {
when(it.isEmpty()) {
true -> Rx2Apollo.from(apolloClient.query(GetCountryListQuery()))
.map { countryMapper.transformToDomain(it.data()?.countryList()) }
.map { saveCountries(it) }
false -> Observable.just(countries)
}
}
private fun saveCountries(items: List<Country>): List<Country> {
if (countries.isNotEmpty()) {
(countries as MutableList).clear()
}
(countries as MutableList).addAll(items)
return countries
}