RxAndroid 处理 observable 中的 switch case
RxAndroid to handle the switch case in observable
我的 android 应用程序必须使用套接字服务以异步方式处理不同的命令和响应,因为这些命令需要调用应用程序中的其他组件并等待异步结果。
我尝试使用 rxjava 将传入的字符串映射到不同的参数,但仍然必须手动路由不同的命令,是否有任何运算符将逻辑路由到自定义的可观察对象并在单个订阅者中处理结果?
代码如下:
Observable.just(command_string)
.map(new Func1<String, Command>() {
@Override
public Command call(String command_string) {
......;
}
})
.filter(new Func1<Command, Boolean>() {
@Override
public Boolean call(Command file) {
ThreadUtils.logThreadSignature(TAG);
return !isExcuetingCmd;
}
})
.subscribeOn(Schedulers.io())
.map(new Func1<Command, CmdResponse>() {
@Override
public CmdResponse call(Command command) {
//stupid code...
switch (cmd)
{
case CMD_1:
...
CmdResponse = xxx
break;
case CMD_2:
...
CmdResponse = xxx
break;
case CMD_3:
...
CmdResponse = xxx
break;
}
return CmdResponse;
}
})
.flatMap(new Func1<CmdResponse, Observable<String>>() {
@Override
public Observable<String> call(CmdResponse cmdResponse) {
return Observable.just(gson.toJson(CmdResponse));
}
});
我认为您可以做的是使用 groupBy 运算符,然后根据您的 cmdResponse,您可以将该可观察对象添加到一个组中。最后,当 observable 完成发射时,您可以检查订阅者上的组。
这里是官方文档http://reactivex.io/documentation/operators/groupby.html
检查我做的这个例子
/**
* In this example we create a String/Person group.
* The key of the group is just the String value of the sex of the item.
*/
@Test
public void testGroupBySex() {
Observable.just(getPersons())
.flatMap(listOfPersons -> Observable.from(listOfPersons)
.groupBy(person -> person.sex))
.subscribe(booleanPersonGroupedObservable -> {
switch (booleanPersonGroupedObservable.getKey()) {
case "male": {
booleanPersonGroupedObservable.asObservable()
.subscribe(person -> System.out.println("Here the male:" + person.name));
break;
}
case "female": {
booleanPersonGroupedObservable.asObservable()
.subscribe(person -> System.out.println("Here the female:" + person.name));
break;
}
}
});
}
private List<Person> getPersons() {
List<Person> people = new ArrayList<>();
people.add(new Person("Pablo", 34, "male"));
people.add(new Person("Paula", 35, "female"));
return people;
}
您可以在此处查看更多示例https://github.com/politrons/reactive
我的 android 应用程序必须使用套接字服务以异步方式处理不同的命令和响应,因为这些命令需要调用应用程序中的其他组件并等待异步结果。
我尝试使用 rxjava 将传入的字符串映射到不同的参数,但仍然必须手动路由不同的命令,是否有任何运算符将逻辑路由到自定义的可观察对象并在单个订阅者中处理结果?
代码如下:
Observable.just(command_string)
.map(new Func1<String, Command>() {
@Override
public Command call(String command_string) {
......;
}
})
.filter(new Func1<Command, Boolean>() {
@Override
public Boolean call(Command file) {
ThreadUtils.logThreadSignature(TAG);
return !isExcuetingCmd;
}
})
.subscribeOn(Schedulers.io())
.map(new Func1<Command, CmdResponse>() {
@Override
public CmdResponse call(Command command) {
//stupid code...
switch (cmd)
{
case CMD_1:
...
CmdResponse = xxx
break;
case CMD_2:
...
CmdResponse = xxx
break;
case CMD_3:
...
CmdResponse = xxx
break;
}
return CmdResponse;
}
})
.flatMap(new Func1<CmdResponse, Observable<String>>() {
@Override
public Observable<String> call(CmdResponse cmdResponse) {
return Observable.just(gson.toJson(CmdResponse));
}
});
我认为您可以做的是使用 groupBy 运算符,然后根据您的 cmdResponse,您可以将该可观察对象添加到一个组中。最后,当 observable 完成发射时,您可以检查订阅者上的组。
这里是官方文档http://reactivex.io/documentation/operators/groupby.html
检查我做的这个例子
/**
* In this example we create a String/Person group.
* The key of the group is just the String value of the sex of the item.
*/
@Test
public void testGroupBySex() {
Observable.just(getPersons())
.flatMap(listOfPersons -> Observable.from(listOfPersons)
.groupBy(person -> person.sex))
.subscribe(booleanPersonGroupedObservable -> {
switch (booleanPersonGroupedObservable.getKey()) {
case "male": {
booleanPersonGroupedObservable.asObservable()
.subscribe(person -> System.out.println("Here the male:" + person.name));
break;
}
case "female": {
booleanPersonGroupedObservable.asObservable()
.subscribe(person -> System.out.println("Here the female:" + person.name));
break;
}
}
});
}
private List<Person> getPersons() {
List<Person> people = new ArrayList<>();
people.add(new Person("Pablo", 34, "male"));
people.add(new Person("Paula", 35, "female"));
return people;
}
您可以在此处查看更多示例https://github.com/politrons/reactive