gpt4 book ai didi

android - RxJava如何过滤子树然后返回树

转载 作者:行者123 更新时间:2023-11-30 01:18:32 24 4
gpt4 key购买 nike

如果我有一个 Observable<JsonOrderList> .

JsonOrderList定义如下:

public class JsonOrderList {

public List<Order> data;
public int currentCount;
public int currentPage;
public int pageCount;

}

API 定义如下:

Observable<JsonOrderList> orderApi.orderList(before, after, limit, state)

我要过滤/处理List<Order> data然后返回 JsonOrderList

orderApi.orderList(before, after, limit, state){
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
//How to process List<Order> data

.subscribe(new Action1<JsonOrderList>() {
@Override
public void call(JsonOrderList rpOrderList) {
pushSuccessData(ID_GET_ORDER_LIST, rpOrderList);
}
}, new Action1<Throwable>() {
@Override
public void call(Throwable throwable) {
pushErrorData(ID_GET_ORDER_LIST, throwable);
}
}));

编辑:

doOnNext(new Action1<JsonOrderList>() {
@Override
public void call(JsonOrderList jsonOrderList) {
//can i use rxjava operator replace loop
for(Order order : jsonOrderList.data){
order.setBuyer(goodsApi.getBuyer(order.getBuyerId));
}
}
})

goodsApi.getBuyer(id) 请求服务器获取BuyerInfo

Could I use rxjava operator replace loop??

最佳答案

使用.map操作符:

orderApi.orderList(before, after, limit, state)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.map(new Func1<List<Order>, JsonOrderList>() {
@Override
public JsonOrderList call(List<Order> orders) {
JsonOrderList jsonOrderList = new JsonOrderList();
// ... do some stuff to fill jsonOrderList
return jsonOrderList;
}
})
.subscribe(new Action1<JsonOrderList>() {
@Override
public void call(JsonOrderList rpOrderList) {
pushSuccessData(ID_GET_ORDER_LIST, rpOrderList);
}
}, new Action1<Throwable>() {
@Override
public void call(Throwable throwable) {
pushErrorData(ID_GET_ORDER_LIST, throwable);
}
});

编辑:

经过对问题的进一步解释,我建议你使用下面的代码。

我建议您不要使用 .filter(...) 运算符,因为它会改变您的流(您会在 subscribe 中获得不同的对象)。

orderApi.orderList(before, after, limit, state)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.doOnNext(new Action1<JsonOrderList>() {
@Override
public void call(JsonOrderList jsonOrderList) {
//do something with jsonOrderList.data
}
})
.subscribe(new Action1<JsonOrderList>() {
@Override
public void call(JsonOrderList rpOrderList) {
pushSuccessData(ID_GET_ORDER_LIST, rpOrderList);
}
}, new Action1<Throwable>() {
@Override
public void call(Throwable throwable) {
pushErrorData(ID_GET_ORDER_LIST, throwable);
}
});

关于android - RxJava如何过滤子树然后返回树,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37478319/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com