I want to split my observable like if/else
statement
Something like:
A[] array = new A[10];
for (int i = 0; i < array.length; i++) {
array[i] = new A(getRandomString(), getRandomInt());
}
Observable.from(array)
.filter(a -> condition(a))
.// <--- do stuff if condition returns true
.// <- back to parent
.filter(a -> complexCondition(a)) // filter all elements(!)
.// <--- do stuff if complex condition returns true
.// <- back to iterate all elements
Is it even possible?
There are two key types to understand when working with Rx: Observable represents any object that can get data from a data source and whose state may be of interest in a way that other objects may register an interest. An observer is any object that wishes to be notified when the state of another object changes.
Code your next android app using RxJava Use of Rx operators. With RxOperator are basically a function that defines the observable, how and when it should emit the data stream. There are hundreds of operators available in RxJava. You can read an alphabetical list of all the operators available from here.
Single is an Observable that always emit only one value or throws an error. A typical use case of Single observable would be when we make a network call in Android and receive a response. Sample Implementation: The below code always emits a Single user object. We use a Single Observable and a Single Observer.
RxJava is a JVM library that uses observable sequences to perform asynchronous and event-based programming. Its primary building blocks are triple O's, which stand for Operator, Observer, and Observables. And we use them to complete asynchronous tasks in our project. It greatly simplifies multithreading in our project.
In Observable does not work in that way, you have to think that the observable is just like a stream, where you can end up with some items, u others. The most close if/else that you will find in observables will be use GroupsBy.
Here you have some practicle example that I did explaining how works
https://github.com/politrons/reactive/blob/master/src/test/java/rx/observables/transforming/ObservableGroupBy.java
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With