Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/208.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 用于切换方法的RxJava运算符_Android_Kotlin_Rx Java_Rx Android - Fatal编程技术网

Android 用于切换方法的RxJava运算符

Android 用于切换方法的RxJava运算符,android,kotlin,rx-java,rx-android,Android,Kotlin,Rx Java,Rx Android,我是Android项目上Rxjava的新手,这里是我的代码 class RadioListRepositoryImpl(private val apiServices: ApiServices, private val chlDao: ChannelDao) : RadioListRepository { private val results: MutableList<DataResponse> init { results = ArrayList<DataRes

我是Android项目上Rxjava的新手,这里是我的代码

class RadioListRepositoryImpl(private val apiServices: ApiServices, private val chlDao: ChannelDao) : RadioListRepository {

private val results: MutableList<DataResponse>

init {
    results = ArrayList<DataResponse>()
}

override fun getData(): Observable<DataResponse> {
    return dataFromMemory().switchIfEmpty(dataFromNetwork())
}

override fun dataFromMemory(): Observable<DataResponse> {

    val cacheDateExp = DateTime().minusHours(6)

    if(chlDao.isCacheExpired(cacheDateExp).isNotEmpty()){

        Logger.d("Get data from cache SQLITE")

        val chList: MutableList<DataResponse> = ArrayList()

        val cache = chlDao.loadAll()
        repeat(cache.size){ i ->
            val ch = DataResponse()
            ch.channelId = cache[i].channelId
            ch.channelTitle = cache[i].title

            chList.add(ch)
        }

        return Observable.from(chList)
    }else{
        chlDao.deleteAll()

        return Observable.empty<DataResponse>()
    }
}

override fun dataFromNetwork(): Observable<DataResponse> {
    val dttime = DateTime()
    return apiServices.getChannelList()
            .concatMap {
                dataListResponseModel -> Observable.from(dataListResponseModel.radio)
            }
            .doOnNext {
                channelDataResponse -> results.add(channelDataResponse)
            }
            .doOnNext { channelDataResponse ->
                Logger.d("Put data to cache")
                val c: ChannelEntitiy = ChannelEntitiy()
                c.channelId = channelDataResponse.channelId
                c.title = channelDataResponse.channelTitle
                chlDao.insert(c)
            }
}
类RadioListRepositoryImpl(私有val apiServices:apiServices,私有val chlDao:ChannelDao):RadioListRepository{
私有val结果:可变列表
初始化{
结果=ArrayList()
}
override fun getData():可观察{
返回dataFromMemory().switchIfEmpty(dataFromNetwork())
}
重写fun dataFromMemory():可观察{
val cacheDateExp=DateTime().minusHours(6)
if(chlDao.isCacheExpired(cacheDateExp.isNotEmpty()){
Logger.d(“从缓存SQLITE获取数据”)
val chList:MutableList=ArrayList()
val cache=chlDao.loadAll()
重复(cache.size){i->
val ch=DataResponse()
ch.channelId=cache[i].channelId
ch.channelTitle=缓存[i]。标题
chList.add(ch)
}
可观察的返回。从(chList)
}否则{
chlDao.deleteAll()
返回可观察的.empty()
}
}
覆盖fun dataFromNetwork():可观察{
val dttime=DateTime()
返回apiServices.getChannelList()
.concatMap{
dataListResponseModel->Observable.from(dataListResponseModel.radio)
}
doOnNext先生{
channelDataResponse->results.add(channelDataResponse)
}
.doOnNext{channelDataResponse->
Logger.d(“将数据放入缓存”)
val c:ChannelEntitiy=ChannelEntitiy()
c、 channelId=channelDataResponse.channelId
c、 title=channelDataResponse.channelTitle
插入(c)
}
}
}

我的另一个类访问方法getData(),我想知道,如果内存中的数据是空的(sqlite),那么就从网络中获取数据

但我想要的是,若内存中的数据是空的,那个么从网络中获取数据,插入内存,然后getData()方法返回dataFromMemory()


我可以用另一个Rx操作员来简化我的代码吗?

当你想从多个来源获取数据时,
concat
first
应该适合你

// Our sources (left as an exercise for the reader)
Observable<Data> memory = ...;  
Observable<Data> disk = ...;  
Observable<Data> network = ...;

// Retrieve the first source with data
Observable<Data> source = Observable  
  .concat(memory, disk, network)
  .first();

检查我的答案
Observable<Data> networkWithSave = network.doOnNext(data -> {  
  saveToDisk(data);
  cacheInMemory(data);
});

Observable<Data> diskWithCache = disk.doOnNext(data -> {  
  cacheInMemory(data);
});