java 如何在 RxJava 2 中使用 Flowable?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/39207964/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to use Flowable in RxJava 2?
提问by anand gaurav
There is an introduction of new Flowable in RxJava2. How to use this in android. There was no Flowable in RxJava1.
RxJava2 中引入了新的 Flowable。如何在android中使用它。RxJava1 中没有 Flowable。
采纳答案by Amit Shekhar
public class FlowableExampleActivity extends AppCompatActivity {
private static final String TAG = FlowableExampleActivity.class.getSimpleName();
Button btn;
TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_example);
btn = (Button) findViewById(R.id.btn);
textView = (TextView) findViewById(R.id.textView);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
doSomeWork();
}
});
}
/*
* simple example using Flowable
*/
private void doSomeWork() {
Flowable<Integer> observable = Flowable.just(1, 2, 3, 4);
observable.reduce(50, new BiFunction<Integer, Integer, Integer>() {
@Override
public Integer apply(Integer t1, Integer t2) {
return t1 + t2;
}
}).subscribe(getObserver());
}
private SingleObserver<Integer> getObserver() {
return new SingleObserver<Integer>() {
@Override
public void onSubscribe(Disposable d) {
Log.d(TAG, " onSubscribe : " + d.isDisposed());
}
@Override
public void onSuccess(Integer value) {
Log.d(TAG, " onSuccess : value : " + value);
}
@Override
public void onError(Throwable e) {
Log.d(TAG, " onError : " + e.getMessage());
}
};
}
}
I have a create a sample project to demonstrate the use of RxJava2. Here you can find the sample project - RxJava2-Android-Samples
我创建了一个示例项目来演示 RxJava2 的使用。在这里你可以找到示例项目 - RxJava2-Android-Samples
回答by Hossein Shahdoost
This is what it says in the documentations
这是它在文档中所说的
Practically, the 1.x fromEmitter (formerly fromAsync) has been renamed to Flowable.create. The other base reactive types have similar create methods (minus the backpressure strategy).
实际上,1.x fromEmitter(以前的fromAsync)已重命名为Flowable.create。其他基本反应类型具有类似的创建方法(减去背压策略)。
So you can use this in the same way as fromEmitter
and fromAsync
所以你可以像fromEmitter
和一样使用它fromAsync