Skip to content

Instantly share code, notes, and snippets.

@tomoima525
Created November 15, 2017 04:20
Show Gist options
  • Select an option

  • Save tomoima525/159cfc17a97acf0ba698726f6b67cc41 to your computer and use it in GitHub Desktop.

Select an option

Save tomoima525/159cfc17a97acf0ba698726f6b67cc41 to your computer and use it in GitHub Desktop.
Storing state
@Override
public Completable getArticles() {
return remoteDataSource.getArticles()
.flatMapCompletable(articles -> localDataSource
.saveArticles(articles);
}
public Observable<List<Article>> observeArticles() {
return localDataSource.observeArticles();
}
BehaviorProcessor<List<Article>> articleList = BehaviorProcessor.create();
LocalDataSource(ArticleDao articleDao) {
this.articleDao = articleDao;
articleDao.subscribeOn(Schedulars.io()).getAll().subscribe(articleList::onNext)
}
public Completable saveArticles(List<Article> article) {
return Completable.fromAction(() -> articleDao.insert(article))
.doOnSuccess(articleList::onNext);
}
public Observable<List<Article>> observeArticles() {
return articleList;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment