使用foreach循环对RxJava请求链进行改造
我正在尝试从使用普通改型迁移到使用RxJava扩展进行改型,以便在后台线程上进行API调用链 例如,我有一个名为ModelGroup的对象,它有一个ModelPerson对象列表。我的目标是做到以下几点使用foreach循环对RxJava请求链进行改造,java,android,retrofit,rx-java,rx-java2,Java,Android,Retrofit,Rx Java,Rx Java2,我正在尝试从使用普通改型迁移到使用RxJava扩展进行改型,以便在后台线程上进行API调用链 例如,我有一个名为ModelGroup的对象,它有一个ModelPerson对象列表。我的目标是做到以下几点 将ModelGroup发送到服务器并接收一个响应,它是一个整数,表示新插入的ID,我们称之为newGroupId 对于ModelGroup中的每个ModelPerson,将Person.groupId设置为newGroupId 将每个人发送到服务器 如果ModelGroup中的所有ModelPe
private void makeGroupInsert(ModelGroup modelGroup) {
int newGroupId = myApi.insertNewGroup(modelGroup.getName(), modelGroup.getRating())
.execute()
.body();
for (ModelPerson person : modelGroup.getPersons()) {
person.setGroupId(newGroupId);
String response = myApi.insertNewPerson(
person.getGroup_id(),
person.getFirst_Name(),
person.getLast_Name())
.execute()
.body();
if (!response.equals("success")) {
// One failed to update, send error to main thread.
}
}
// All succeeded, send success back to main thread.
}
问题
如何使用RxJava+改造解决方案实现相同(或更好)的功能
编辑1
MyApi的定义如下
public interface MyApi {
@POST("insert_new_group")
Call<Integer> insertNewGroup(@Query("group_name") String groupName,
@Query("group_rating") int rating);
@POST("insert_new_person")
Call<String> insertNewPerson(@Query("group_id") int groupId,
@Query("first_name") String firstName,
@Query("last_name") String lastName);
}
公共接口MyApi{
@邮政(“插入新集团”)
调用insertNewGroup(@Query(“group_name”)字符串groupName,
@查询(“集团评级”)内部评级;
@职位(“插入新人”)
调用insertNewPerson(@Query(“group_id”)int-groupId,
@查询(“first_name”)字符串firstName,
@查询(“姓氏”)字符串(姓氏);
}
首先,您需要将改造bean更改为使用可观察对象。例如,它可以类似于以下行:
@POST("insert_new_group")
Observable<Integer> insertNewGroup(...
@POST(“插入新组”)
可观测insertNewGroup(。。。
然后您可以链接请求:
void updateData() {
myApi.insertNewGroup(modelGroup.getName(), modelGroup.getRating()) //Creating new group and getting its ID
.switchMap(this::setGroupIdAll) //Calling observable that will loop thru all persons and set their groupIDs
.subscribe(
(n) -> {/*you will get String after every 'insertNewPerson' run*/},
(e) -> {/*error handling*/}
);
}
Observable<String> setGroupIdAll(Integer id) {
return Observable.fromIterable(personsIterable) //personsIterable contains all your ModelPerson objects
.flatMap(this::updatePerson); //Call Observabl;e that will send updated person to the server
}
Observable<String> updatePerson(ModelPerson person) {
return myApi.insertNewPerson(
person.getGroup_id(),
person.getFirst_Name(),
person.getLast_Name());
}
void updateData(){
myApi.insertNewGroup(modelGroup.getName(),modelGroup.getRating())//创建新组并获取其ID
.switchMap(this::setGroupIdAll)//调用observable,它将循环遍历所有人员并设置其GroupID
.订阅(
(n) ->{/*您将在每次运行“insertNewPerson”后获得字符串*/},
(e) ->{/*错误处理*/}
);
}
可观测setGroupIdAll(整数id){
return Observable.fromIterable(personsIterable)//personsIterable包含所有ModelPerson对象
.flatMap(this::updatePerson);//调用observable;e,将更新的person发送到服务器
}
可观察更新人员(ModelPerson){
返回myApi.insertNewPerson(
person.getGroup_id(),
person.getFirst_Name(),
person.getLast_Name());
}
如果您包含更多实际涉及此问题的代码,例如改装api等,则更容易提供帮助。感谢您的回答。现在我的应用程序是被动的)