Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

concat in RxJava2 and first() function

I'm migrating my repository pattern to RxJava2. My old code look like

@Override
public Observable<List<Post>> getPost() {
    return Observable.concat(mAppLocalDataStore.getPost(), mAppRemoteDataStore.getPost())
            .first(new Func1<List<Post>, Boolean>() {
                @Override
                public Boolean call(List<Post> posts) {
                    return posts != null;
                }
            });
}

However, I don't know how to replace it in RxJava2, how to call posts.upToDate() with my current code?

@Override
public Observable<List<Post>> getPost() {
    List<Post> list = new ArrayList<>();
    return Observable.concat(mAppLocalDataStore.getPost(), mAppRemoteDataStore.getPost())
            .first(list)
            .toObservable();
}
like image 234
qbait Avatar asked Feb 10 '26 11:02

qbait


1 Answers

Thank you @PhoenixWang for the hint. This is the solution.

@Override
public Observable<List<Post>> getPost() {
    return Observable.concat(mAppLocalDataStore.getPost(), mAppRemoteDataStore.getPost())
            .filter(new Predicate<List<Post>>() {
                @Override
                public boolean test(@NonNull List<Post> posts) throws Exception {
                    return isUpToDate(posts);
                }
            });
}
like image 84
qbait Avatar answered Feb 13 '26 05:02

qbait