rxjava-拆分并组合可观察的



我是rxjava的新手,需要有关如何执行以下操作的帮助/指导:

我需要从可观察的两个值

获得两个值
  • 字符串
  • 列表< .objecta>

我需要在此列表中应用两个不同的过滤器(),然后 最终将它们全部(字符串,FilteredLista,FilteredListB)组合到 一个可观察的。

我可以使用一个链式呼叫吗?

以下是执行相同的示例代码。

MasterObject = String, List<.ObjectA>
 Observable<ReturnObject> getReturnObject() { 
        Observable<MasterObject> masterObjectObservable = getMasterObjectObservable();

        Observable<String> myStringbservable = masterObjectObservable.map(new Func1<MasterObject, String>() {
            @Override
            public String call(MasterObject masterObject) {
                return masterObject.getString();
            }
        });
        return  masterObjectObservable.flatMap(new Func1<MasterObject, Observable<ObjectA>>() {
            @Override
            public Observable<MasterObject> call(MasterObject masterObject) {
                return Observable.from(masterObject.getList());
            }
        }).filter(new Func1<ObjectA, Boolean>() {
            @Override
            public Boolean call(ObjectA objectA) {
                return objectA.isTrue();
            }
        }).toList().concatWith(getSecondList(masterObjectObservable)).zipWith(publicKeyObservable, new Func2<List<ObjectA>, String, ReturnObject>() {
            @Override
            public ReturnObject call(List<ObjectA> listObjectA, String string) {
                return new ReturnObject(string, listObject);
            }
        });
}

private Observable<List<ObjectA>> getSecondList(Observable<MasterObject> masterObject) {
   return masterObject.flatMap(new Func1<MasterObject, Observable<ObjectA>>() {
        @Override
        public Observable<ObjectA> call(MasterObject masterObject) {
            return Observable.from(masterObject.getList());
        }
    }).filter(new Func1<ObjectA, Boolean>() {
        @Override
        public Boolean call(ObjectA objectA) {
            return objectA.isCondition();
        }
    }).toSortedList(new Func2<ObjectA, ObjectA, Integer>() {
        @Override
        public Integer call(ObjectA a, ObjectA b) {
            return a.getCondition()
                    .compareTo(b.getCondition());
        }
    });
}

这是groupBy

的示例
public class Multikind2 {
    static Observable<Object> getSource() {
        return Observable.just("String", Arrays.asList(1, 2, 3, 4));
    }
    enum ValueKind {
         STRING, LIST
    }
    public static void main(String[] args) {
        Func1<Object, ValueKind> kindSelector = o -> {
            if (o instanceof String) {
                return ValueKind.STRING;
            } else
            if (o instanceof List) {
                return ValueKind.LIST;
            }
            throw new IllegalArgumentException("Unsupported value: "+ o);
        };
        getSource()
        .groupBy(kindSelector)
        .flatMap(g -> {
            if (g.getKey() == ValueKind.STRING) {
                return g;
            } else
            if (g.getKey() == ValueKind.LIST) {
                Observable<Integer> flattened = g
                        .flatMapIterable(v -> (Iterable<Integer>)v)
                        .share();
                Observable<Integer> filtered1 = flattened
                        .filter(v -> v % 2 == 0);
                Observable<String> filtered2 = flattened
                        .filter(v -> v % 2 != 0)
                        .map(v -> "-> " + v);
                return filtered1.cast(Object.class)
                        .mergeWith(filtered2).cast(Object.class);
            }
            return Observable.<Object>error(
                new IllegalArgumentException("Unsupported value kind: "+ g.getKey()));
        }).subscribe(
            System.out::println, 
            Throwable::printStackTrace, 
            () -> System.out.println("Done"));
    }
}

相关内容

  • 没有找到相关文章