RxJava - Observables:一个Observable而不是返回其他Observable

时间:2016-10-20 13:41:04

标签: rx-java

我在应用程序中工作而不是转换其他对象中的一个对象列表,为此,我使用Observable of Rx Java。

我有两个方法,一个是具有asyn服务的适配器(Jersey外观),此方法使用另一个服务一个Observable。

我需要使用一个Observable并在完成此项时处理每个项目我创建了一个处理完所有项目的列表并返回一个新的Observable,其中E是每个处理项目的列表。

要处理每个项目,我使用flatMap运算符,但我不知道如何创建一个新的Observer,它有一个类型diferente,例如一个由flatmap运算符处理的所有列表。

任何想法?

由于

更新

这段代码处理每个Element并返回另一个Observable,但我不知道这是否做得好。

@Override
public Observable<ArrayList> getGeoJson2() {
    WKTReader2 reader2 = new WKTReader2();
    WKBReader wkbReader = new WKBReader();
    ArrayList featureCollection = new ArrayList();

    Subject<ArrayList,ArrayList> subject = PublishSubject.create();

    manzanaRepository.getManzanas().map(new Func1<Manzana, SimpleFeature>() {
        @Override
        public SimpleFeature call(Manzana manzana) {
            try {
                SimpleFeatureType TYPE = DataUtilities.createType("", "geom,name:String");
                return SimpleFeatureBuilder.build( TYPE, new Object[]{ null, "name1"}, null);
            }catch (Exception e){
                System.out.println(e.getMessage());
                return null;
            }

        }
    }).subscribe(new Subscriber<SimpleFeature>() {
        @Override
        public void onCompleted() {
            subject.onNext(featureCollection);
            subject.onCompleted();
        }

        @Override
        public void onError(Throwable throwable) {
            subject.onError(throwable);
        }

        @Override
        public void onNext(SimpleFeature simpleFeature) {
            featureCollection.add(simpleFeature);
        }
    });

    return subject;
}

此代码是使用Observable回归者的代码:

@GET
@Produces(MediaType.APPLICATION_JSON)
@Path("/async/geom")
public void asyncGetGeom(@Suspended final AsyncResponse asyncResponse) {

    Observable<ArrayList> features = service.getGeoJson2();

    features.subscribe(new Observer<ArrayList>() {
        @Override
        public void onCompleted() {

            System.out.println("Se completo la accion!!!");
        }

        @Override
        public void onError(Throwable throwable) {

            System.out.println(throwable.getMessage());
        }

        @Override
        public void onNext(ArrayList features) {
            asyncResponse.resume(features);
        }
    });
}

onNext()方法永远不会被调用!!!

由于

2 个答案:

答案 0 :(得分:1)

Try replacing your getGeoJson2() with this:

@Override
public Observable<List<SimpleFeature>> getGeoJson2() {
    return manzanaRepository.getManzanas()
            .map(new Func1<Manzana, SimpleFeature>() {
                @Override
                public SimpleFeature call(Manzana manzana) {
                    try {
                        SimpleFeatureType TYPE = DataUtilities.createType("", "geom,name:String");
                        return SimpleFeatureBuilder.build( TYPE, new Object[]{ null, "name1"}, null));
                    } catch (Exception e) {
                        System.out.println(e.getMessage());
                        return null;
                    }
                }
            })
            .filter(new Func1<SimpleFeature, Boolean>() {
                @Override
                public Boolean call(SimpleFeature sf) {
                    return sf != null;
                }
            })
            .toList();
}

Explanation: toList() operator is used, which waits for the onCompleted from source observable and then emits all the items emitted by the source observable as a list.

答案 1 :(得分:0)

您的代码的问题在于您使用的是在开始使用之前完成的PublishSubject

如果将其更改为BehaviorSubject,它将起作用

然而,更好(并且更短)实现它如下 -

manzanaRepository.getManzanas().map(/**/).collect(/*create array*/, /*add next item*/)