ReactiveX 响应式编程
An API For asynchronous programming with observable streams
流式响应式编程思维
流式编程
| 开始 | 需求 | 需求 | 需求 | 结束 |
|---|---|---|---|---|
| 子线程 | 子线程 | 子线程 | 主现场 | |
| 被观察者 | 下载 | 排序 | 过滤 | subscribe |
For Example
//开始
Observable
.just(PATH)
//需求
.map(new Function<String, Object>() {
@Override
public Object apply(String s) {
Map<String, Object> map = new ArrayMap<>();
map.put("id", 10006);
map.put("name", "1");
Call<ResponseBody> call = retrofit.create(Api.class).getData3(map);
call.enqueue(new Callback<ResponseBody>() {
@Override
public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
}
@Override
public void onFailure(Call<ResponseBody> call, Throwable throwable) {
}
});
return null;
}
})
//表示订阅在子线程
.subscribeOn(Schedulers.io())
//表示Observer观察者的线程是主线程
.observeOn(AndroidSchedulers.mainThread())
//订阅
.subscribe(new Observer<Object>() {
@Override
public void onSubscribe(@NonNull Disposable d) {
}
@Override
public void onNext(@NonNull Object o) {
}
@Override
public void onError(@NonNull Throwable e) {
}
@Override
public void onComplete() {
}
});