按下按钮RxJava2后如何切换地图
How to switchMap after press button RxJava2
我研究switchMap。
案例是:
1) 用户按下按钮 1
2)View调用Presenter方法getRecord(id),并发送按钮id。
3) Presenter 订阅 observable 并发出数据
4) 用户按下按钮 2
5) 演示者取消订阅当前订阅并创建新订阅。
我知道我必须使用 switchMap 运算符。
我想在日志中看到:Obs1: 0, Obs1: 1, Obs1: 2(click) Obs2: 0, Obs2: 1 ....
这是我的代码
查看:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mPresenter = new Presenter();
Button button1 = findViewById(R.id.btn_1);
Button button2 = findViewById(R.id.btn_2);
button1.setOnClickListener(v -> {
mPresenter.getRecord(1);
});
button2.setOnClickListener(v -> {
mPresenter.getRecord(2);
});
主持人:
public class Presenter {
private final String TAG = this.getClass().getSimpleName();
int ID = 0;
Observable observable = Observable.just(ID);
public void getRecord(int id) {
observable
.doOnNext(data -> Log.d(TAG, String.valueOf(data)))
.switchMap(s -> getObservable(id))
.subscribe(data -> Log.d(TAG, (String) data));
}
Observable<String> getObservable(int id) {
return Observable.interval(1, TimeUnit.SECONDS)
.map(data -> "Obs" + String.valueOf(id) + ":" + data);
}
}
Here is my LOG:
D/Presenter: 0
D/Presenter: Obs1:0
D/Presenter: Obs1:1
D/Presenter: Obs1:2
D/Presenter: Obs1:3
D/Presenter: Obs1:4
D/Presenter: Obs1:5
D/Presenter: 0
D/Presenter: Obs1:6
Obs2:0
D/Presenter: Obs1:7
D/Presenter: Obs2:1
D/Presenter: Obs1:8
D/Presenter: Obs2:2
D/Presenter: Obs1:9
D/Presenter: Obs2:3
D/Presenter: Obs1:10
D/Presenter: Obs2:4
我建议将按钮点击作为一些 UiEvent
的流,在演示者中合并两个按钮流,switchMap
将按预期工作。
伪代码
Observable<UiEvent> getButton1ClickEvents(){
return RxView.clicks(button1)
.map(new Button1ClickEvent());
}
Observable<UiEvent> getButton2ClickEvents(){
return RxView.clicks(button2)
.map(new Button2ClickEvent());
}
void getRecord(){
Observable.merge(getButton1ClickEvents(),getButton2ClickEvents())
.switchMap(event -> getObservable(id))
....
}
我研究switchMap。 案例是: 1) 用户按下按钮 1 2)View调用Presenter方法getRecord(id),并发送按钮id。 3) Presenter 订阅 observable 并发出数据 4) 用户按下按钮 2 5) 演示者取消订阅当前订阅并创建新订阅。
我知道我必须使用 switchMap 运算符。 我想在日志中看到:Obs1: 0, Obs1: 1, Obs1: 2(click) Obs2: 0, Obs2: 1 .... 这是我的代码 查看:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mPresenter = new Presenter();
Button button1 = findViewById(R.id.btn_1);
Button button2 = findViewById(R.id.btn_2);
button1.setOnClickListener(v -> {
mPresenter.getRecord(1);
});
button2.setOnClickListener(v -> {
mPresenter.getRecord(2);
});
主持人:
public class Presenter {
private final String TAG = this.getClass().getSimpleName();
int ID = 0;
Observable observable = Observable.just(ID);
public void getRecord(int id) {
observable
.doOnNext(data -> Log.d(TAG, String.valueOf(data)))
.switchMap(s -> getObservable(id))
.subscribe(data -> Log.d(TAG, (String) data));
}
Observable<String> getObservable(int id) {
return Observable.interval(1, TimeUnit.SECONDS)
.map(data -> "Obs" + String.valueOf(id) + ":" + data);
}
}
Here is my LOG:
D/Presenter: 0
D/Presenter: Obs1:0
D/Presenter: Obs1:1
D/Presenter: Obs1:2
D/Presenter: Obs1:3
D/Presenter: Obs1:4
D/Presenter: Obs1:5
D/Presenter: 0
D/Presenter: Obs1:6
Obs2:0
D/Presenter: Obs1:7
D/Presenter: Obs2:1
D/Presenter: Obs1:8
D/Presenter: Obs2:2
D/Presenter: Obs1:9
D/Presenter: Obs2:3
D/Presenter: Obs1:10
D/Presenter: Obs2:4
我建议将按钮点击作为一些 UiEvent
的流,在演示者中合并两个按钮流,switchMap
将按预期工作。
伪代码
Observable<UiEvent> getButton1ClickEvents(){
return RxView.clicks(button1)
.map(new Button1ClickEvent());
}
Observable<UiEvent> getButton2ClickEvents(){
return RxView.clicks(button2)
.map(new Button2ClickEvent());
}
void getRecord(){
Observable.merge(getButton1ClickEvents(),getButton2ClickEvents())
.switchMap(event -> getObservable(id))
....
}