[英]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.我需要填写 object 的字段,以便将其发布到 API。
I am using rxjava and room but my chain of orders is failling我正在使用 rxjava 和 room 但我的订单链失败了
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我需要创建一个 object,其中包含口袋妖怪的数据以及相关的注释列表
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.我的问题是 subscribeWith onNext 方法永远不会被调用。 My goal it that when onNext is called it should have a list of pokemon and each pokemon should have their notes
我的目标是,当 onNext 被调用时,它应该有一个口袋妖怪列表,每个口袋妖怪都应该有他们的笔记
Thanks谢谢
Below I describe Room-ish way for your task without RxJava下面我描述了在没有 RxJava 的情况下完成任务的 Room-ish 方式
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):然后你可以添加另一个 class (它只是一个 class 没有连接到 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:并将方法添加到您的 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.使用此方法,您将获得包含口袋妖怪和笔记的列表。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.