如何使用RxJava链接多个观察值?
调用activity如何使用RxJava链接多个观察值?,java,android,rx-java,rx-android,Java,Android,Rx Java,Rx Android,调用activityonDestroy时,我正在尝试执行一些操作。我想用一个ID启动一个Observable,然后从Realm中检索一些数据,并根据检索到的数据向后端执行HTTP请求,然后将检索到的数据存储到具有起始ID的行中 摘要: 从id为的数据库检索数据 使用数据执行对后端的请求 将检索到的数据存储到具有步骤1中id的行中 图形化: 代码: 我的结局是什么,我被卡住了 Observable.just(id) .observeOn(Schedulers.io()) .map(新函数(){
onDestroy
时,我正在尝试执行一些操作。我想用一个ID启动一个Observable,然后从Realm中检索一些数据,并根据检索到的数据向后端执行HTTP请求,然后将检索到的数据存储到具有起始ID的行中
摘要:
Observable.just(id)
.observeOn(Schedulers.io())
.map(新函数(){
@凌驾
public Person apply(@NonNull String id)引发异常{
Realm Realm=Realm.getDefaultInstance();
Person-Person=realm.copyFromRealm(realm.where(Person.class).equalTo(“id”,id).findFirst());
realm.close();
返回人;
}
})
.switchMap(新函数(){
@凌驾
公共可观察应用(@NonNull Person)引发异常{
返回Utils.getRemoteService().getDirections(person.getAddress());//改型
}
})
.map(新函数(){
@凌驾
公共对象应用(@NonNull Directions)引发异常{
//如何在此处获取id以将数据存储到正确的人
返回null;
}
})
.subscribe();
注意:
- 波乔是虚构的
- 这是我第一次使用RxJava
Observable.just(id)
.observeOn(Schedulers.io())
.map(new Function<String, Person>() {
@Override
public Person apply(@NonNull String id) throws Exception {
Realm realm = Realm.getDefaultInstance();
Person person = realm.copyFromRealm(realm.where(Person.class).equalTo("id", id).findFirst());
realm.close();
return person;
}
})
.switchMap(new Function<Person, Observable<Directions>>() {
@Override
public Observable<Directions> apply(@NonNull Pair<String, Person> pair) throws Exception {
// assuming that id is available by getId
return Pair(person.getId(), Utils.getRemoteService().getDirections(person.getAddress())); // retrofit
}
})
.map(new Function<Pair<String, Directions>, Object>() {
@Override
public Object apply(@NonNull Pair<String, Directions> pair) throws Exception {
// how do I get the id here to store the data to the correct person
// pair.first contains the id
// pair.second contains the Directions
return null;
}
})
.subscribe();
Observable.just(id)
.observeOn(Schedulers.io())
.map(新函数(){
@凌驾
public Person apply(@NonNull String id)引发异常{
Realm Realm=Realm.getDefaultInstance();
Person-Person=realm.copyFromRealm(realm.where(Person.class).equalTo(“id”,id).findFirst());
realm.close();
返回人;
}
})
.switchMap(新函数(){
@凌驾
公共可观察应用(@NonNull对)引发异常{
//假设getId可以使用该id
返回对(person.getId(),Utils.getRemoteService().getDirections(person.getAddress());//改型
}
})
.map(新函数(){
@凌驾
公共对象应用(@NonNull对)引发异常{
//如何在此处获取id以将数据存储到正确的人
//pair.first包含id
//第二对包含方向
返回null;
}
})
.subscribe();
Observable.just(id)
.observeOn(Schedulers.io())
.map(new Function<String, Person>() {
@Override
public Person apply(@NonNull String id) throws Exception {
Realm realm = Realm.getDefaultInstance();
Person person = realm.copyFromRealm(realm.where(Person.class).equalTo("id", id).findFirst());
realm.close();
return person;
}
})
.switchMap(new Function<Person, Observable<Directions>>() {
@Override
public Observable<Directions> apply(@NonNull Pair<String, Person> pair) throws Exception {
// assuming that id is available by getId
return Pair(person.getId(), Utils.getRemoteService().getDirections(person.getAddress())); // retrofit
}
})
.map(new Function<Pair<String, Directions>, Object>() {
@Override
public Object apply(@NonNull Pair<String, Directions> pair) throws Exception {
// how do I get the id here to store the data to the correct person
// pair.first contains the id
// pair.second contains the Directions
return null;
}
})
.subscribe();