Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RxJava2 Single.Concat for repository pattern

I am using Room with RxJava2 to implement my data layer via Repository Pattern principles.

I have the following simple code which decides where to pick data from.

@Override
    public Single<Team> getTeamById(int teamId) {
        return Single.
                concat(local.getTeamById(teamId),
                        remote.getTeamById(teamId)).
                filter(team -> team != null).
                firstOrError();
    }

The problem here is that instead of going to the remote source , it returns an error from the first source (local) if the data was not available.

android.arch.persistence.room.EmptyResultSetException: Query returned empty result set: select * from teams where id = ?

How should I instruct the concat to forgo any error that is received and continue its concatenation?

like image 617
Muhammad Ahmed AbuTalib Avatar asked Jan 28 '23 11:01

Muhammad Ahmed AbuTalib


2 Answers

Aslong you're not sure if you can receive at least one Team from you data provider, you should probably think of using Maybe instead of Single.

You can lookup the definition here:

Single as it states:

it always either emits one value or an error notification

Use Maybe instead: Maybe

there could be 0 or 1 item or an error signalled by some reactive source

As your error already states there seems to be a problem while extracting results from your query.

Handle your result extraction correctly, so that you check if there are results before trying extracting any. Therefor the Maybe would either return 0 or 1 item, and not throw any error at all when no Team was found.

like image 91
TheWhiteLlama Avatar answered Jan 31 '23 02:01

TheWhiteLlama


You cannot pass null in RxJava2. So whenever your local repo is empty you just can't return null in your single. There was a question o stack about handling null objects: Handle null in RxJava2

Also here you can find an article showing you preferred implementation of repository pattern using RxJava2: https://android.jlelse.eu/rxjava-2-single-concat-sample-for-repository-pattern-1873c456227a

So simplifying - instead of returning null from both local and remote repo pass some sort of "empty" object. That will be useful also in your business logic allowing you to recognize empty set of data.

like image 40
muminers Avatar answered Jan 31 '23 00:01

muminers