简体   繁体   English

RxJava为项列表创建可观察链

[英]RxJava create observable chain for list of items

How do I return an observable which will emit multiple items after performing a webservice call for all items in a List? 如何在对列表中的所有项目执行webservice调用后返回将发出多个项目的observable?

So I have a webservice which saves for example a single "car". 所以我有一个网络服务,可以节省一个单一的“汽车”。 I retrieve a list of "cars" from the UI and I want to save all these individually but I only want to subscribe to 1 Observable. 我从UI中检索“汽车”列表,我想单独保存所有这些,但我只想订阅1 Observable。 So I basically want to create some sort of chain of multiple Observables depending on the amount of "cars" received from the UI and I want every result of the webservie emitted by the single Observable. 所以我基本上想要创建一些多个Observable链,这取决于从UI收到的“汽车”数量,我希望单个Observable发出的webservie的每个结果。 Once all the "cars" are saved int he webservice the single Observable may onComplete. 一旦将所有“汽车”保存在web服务中,单个Observable就可以onComplete。

I am using Retrofit for calling web service with RxJava like this : 我正在使用Retrofit来调用RxJava的Web服务,如下所示:

Request : 要求:

Observable<ResponsePOJO> getCars(Param param);

Call getCars and handle response : 调用getCars并处理响应:

client.getCars(param)
                .subscribeOn(Schedulers.newThread())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(Response -> {
                   // Success
                }, e -> {
                    // Error
                });

I might be misunderstanding, but isn't this just a case where you could flatmap from the list to the individual items? 我可能会误解,但这不是一个你可以从列表平面映射到单个项目的情况吗?

public Observable<Car> getCars() {
   return api.getCars()
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .flatMap(new Func1<List<Car>, Observable<Car>>() {
                @Override public Observable<Car> call(List<Car> carlist) {
                    return Observable.from(carlist);
                }
            });
}

With this, the onNext(Car car) will be called multiple times and whenever the entire list is handled, onCompleted() is called. 这样,onNext(Car car)将被多次调用,每当处理整个列表时,都会调用onCompleted()。 In this example I used the network as a source for my list, but I imagine this can as well be the UI. 在这个例子中,我使用网络作为我的列表的源,但我想这也可以是UI。

I Hope this is what you are looking for, otherwise, please elaborate and I'll think along with you ;) 我希望这是你正在寻找的,否则,请详细说明,我会和你一起思考;)

I think you just need to use Observable.from and if you need to create an Observable per car just use flatMap operator. 我想你只需要使用Observable.from,如果你需要为每辆车创建一个Observable,只需使用flatMap运算符。 Once you finish you just need to subscribe. 完成后,您只需订阅即可。

@Test
public void flatMapCars() {
    Observable.from(getCars())
            .flatMap(this::saveCar)
            .subscribe();
}

private List<String> getCars() {
    return Arrays.asList("Aston martin", "Renault", "Seat");
}

private Observable<String> saveCar(String car) {
    return Observable.just(car);//You should save the car
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM