Shura Capricorn
Shura Capricorn

Reputation: 129

How to chain queries with rxjava and room

I need to fill the fields of an object in order to post it to an API.

I am using rxjava and room but my chain of orders is failling

My daos

    @Dao
abstract public class PokemonDao implements BaseDao<Pokemon>{
    @Query("SELECT * FROM pokemons ORDER BY id ASC")
    abstract public Flowable<List<Pokemon>> getAll();
}

    @Dao
abstract public class NoteDao implements BaseDao<Note>{


    @Query("SELECT * FROM notes WHERE idPokemon = :idPokemon ORDER BY registerDate DESC")
    abstract public Flowable<List<Note>> getNotes(int idPokemon);
}

I need to create an object that has the data of the pokemon with a list of notes associated

I did the following on my viewmodel

            pokemonRepository.getFavourites()
                    .toObservable()
                .flatMap(new Function<List<Pokemon>, ObservableSource<?>>() {
                    @Override
                    public ObservableSource<?> apply(List<Pokemon> favourites) throws Exception {
                        return Observable.fromIterable(favourites);
                    }
                })
    .flatMap(new Function<Object, ObservableSource<?>>() {
                @Override
                public ObservableSource<?> apply(Object o) throws Exception {
                    return getNotesObservable((Favourite) o);
                }
            })
    .toList()
.toObservable()
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())

                    .subscribeWith(new SingleObserver<List<Object>>() {
                @Override
                public void onSubscribe(Disposable d) {

                }

                @Override
                public void onSuccess(List<Object> objects) {

                }

                @Override
                public void onError(Throwable e) {

                }
            })

I also use this method

private Observable<Object> getNotesObservable(Pokemon favourite) {


    Observable<Object> lolo = noteRepository.getNotes(Integer.parseInt(favourite.getId()))
            .map(new Function<List<Note>, Object>() {
                @Override
                public Favourite apply(List<Note> notes) throws Exception {
                    favourite.notesList= notes;
                    return favourite;
                }
            })
            .toObservable();

    return lolo;

}

My problem is that on the subscribeWith onNext method is never called. My goal it that when onNext is called it should have a list of pokemon and each pokemon should have their notes

Thanks

Upvotes: 0

Views: 529

Answers (1)

sergiy tykhonov
sergiy tykhonov

Reputation: 5103

Below I describe Room-ish way for your task without RxJava

Let's say you have these entities:

@Entity
public class Pokemon {
    @PrimaryKey public int id;
    public String name;
    // other fields
    ........
}

@Entity
public class Note {
    @PrimaryKey public int noteId;
    public int pokemonId;
    // other fields
    ........ 
}

Then you can add another class (it's just a class with no connection to SQLite):

public class PokemonWithNotes {
    @Embedded public Pokemon pokemon; // here you'll get your pokemon
    @Relation(
         parentColumn = "id",
         entityColumn = "pokemonId"
    )
    public List<Note> notesList; // here you'll het your notes' list
}

and add method to your dao:

@Transaction
@Query("SELECT * FROM Pokemon")
public List<PokemonWithNotes> getPokemonListWithNotes();

Room orders to this method to get both Pokemons and Notes and connect them (without two queries)

Using this method you'll get your List with Pokemons and notes.

Upvotes: 1

Related Questions