如何使用 rxjava 和 room 链接查询
How to chain queries with rxjava and room
我需要填写一个对象的字段,以便 post 将它变成 API。
我正在使用 rxjava 和 room 但我的订单链失败了
我的道
@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);
}
我需要创建一个对象,其中包含口袋妖怪的数据以及关联的笔记列表
我在我的视图模型上做了以下操作
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) {
}
})
我也在用这个方法
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;
}
我的问题是永远不会调用 subscribeWith onNext 方法。
我的目标是当 onNext 被调用时它应该有一个 pokemon 列表并且每个 pokemon 应该有他们的笔记
谢谢
下面我描述了在没有 RxJava 的情况下完成任务的 Room-ish 方式
假设您有这些实体:
@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
........
}
然后你可以添加另一个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
}
并将方法添加到您的 dao 中:
@Transaction
@Query("SELECT * FROM Pokemon")
public List<PokemonWithNotes> getPokemonListWithNotes();
房间订单到此方法同时获取口袋妖怪和笔记并连接它们(无需两次查询)
使用此方法,您将获得包含口袋妖怪和笔记的列表。
我需要填写一个对象的字段,以便 post 将它变成 API。
我正在使用 rxjava 和 room 但我的订单链失败了
我的道
@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);
}
我需要创建一个对象,其中包含口袋妖怪的数据以及关联的笔记列表
我在我的视图模型上做了以下操作
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) {
}
})
我也在用这个方法
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;
}
我的问题是永远不会调用 subscribeWith onNext 方法。 我的目标是当 onNext 被调用时它应该有一个 pokemon 列表并且每个 pokemon 应该有他们的笔记
谢谢
下面我描述了在没有 RxJava 的情况下完成任务的 Room-ish 方式
假设您有这些实体:
@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
........
}
然后你可以添加另一个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
}
并将方法添加到您的 dao 中:
@Transaction
@Query("SELECT * FROM Pokemon")
public List<PokemonWithNotes> getPokemonListWithNotes();
房间订单到此方法同时获取口袋妖怪和笔记并连接它们(无需两次查询)
使用此方法,您将获得包含口袋妖怪和笔记的列表。