gpt4 book ai didi

android - RXJava - 拆分和合并 Observable

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:00:54 26 4
gpt4 key购买 nike

我是 RxJava 的新手,需要一些关于如何执行以下操作的帮助/指导:

I need to get two values from an Observable

  • a String
  • a List<.ObjectA>

I then need to apply two different filters() on this list and then finally combine all of them(String, FilteredListA, FilteredListB) into a single observable.

我可以使用单个链式调用吗???(可能需要 groupBy 的示例)

下面是执行相同操作的示例代码。

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"));
}
}

关于android - RXJava - 拆分和合并 Observable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30202986/

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