繁体   English   中英

匕首2清单重新创建

[英]Dagger 2 list re-creation

我遇到了使用Dagger2保存“缓存”数据的问题。

我有Application ComponentAuthentication 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
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM