从网络和数据库 rxJava 获取请求
Get request from network and dataBase rxJava
我会从数据库获取请求,同时从网络获取请求。从网络请求保存在数据库中,我想但它不正确
fun loadMovie(id: Int): Single<AboutMovie> = database.getMovie(id = id)
.onErrorResumeNext( aboutMovieService.getMovie(movieId = id) )
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
我不完全理解您要实现的目标,但让我为您提供该代码。
fun loadMovie(id: Int): Single<Movie> =
database.getMovie(id = id) // get cached entry from database
.onErrorResumeNext { // if any error occured
getNetworkMovie(id = id) // do network request
.doOnSuccess { database.saveMovie(movie) } // on network request success save to database
}
我会从数据库获取请求,同时从网络获取请求。从网络请求保存在数据库中,我想但它不正确
fun loadMovie(id: Int): Single<AboutMovie> = database.getMovie(id = id)
.onErrorResumeNext( aboutMovieService.getMovie(movieId = id) )
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
我不完全理解您要实现的目标,但让我为您提供该代码。
fun loadMovie(id: Int): Single<Movie> =
database.getMovie(id = id) // get cached entry from database
.onErrorResumeNext { // if any error occured
getNetworkMovie(id = id) // do network request
.doOnSuccess { database.saveMovie(movie) } // on network request success save to database
}