Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use Flowable in RxJava 2?

There is an introduction of new Flowable in RxJava2. How to use this in android. There was no Flowable in RxJava1.

like image 429
anand gaurav Avatar asked Aug 29 '16 13:08

anand gaurav


People also ask

What is RxJava flowable?

RxJava is a Reactive Extensions Java implementation that allows us to write event-driven, and asynchronous applications.

What is flowable in RxJava Android?

Flowable: emit a stream of elements (endlessly, with backpressure) Single: emits exactly one element. Maybe: emits zero or one elements. Completable: emits a “complete” event, without emitting any data type, just a success/failure.

What is difference between flowable and Observable?

Observables are used when we have relatively few items over the time and there is no risk of overflooding consumers. If there is a possibility that the consumer can be overflooded, then we use Flowable. One example could be getting a huge amount of data from a sensor. They typically push out data at a high rate.

What RxJava 2?

RxJava is a Java library that enables Functional Reactive Programming in Android development. It raises the level of abstraction around threading in order to simplify the implementation of complex concurrent behavior.


2 Answers

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

like image 95
Amit Shekhar Avatar answered Oct 20 '22 17:10

Amit Shekhar


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).

So you can use this in the same way as fromEmitter and fromAsync

Rx.2 Documentation

like image 40
Hossein Shahdoost Avatar answered Oct 20 '22 15:10

Hossein Shahdoost