Sunday, 6 May 2018

Common RxJava mistakes notes



The stream
- Each time you call the observable, it returns a new version of observable to operate with
- The operation with observable is just a stream of operators
    - Upstream: is anything that created the current operator
    - Downstream: is anything that will create the next operators

Creating observables
- Just(object)
- from(iterable)
- whatever Retrofit is doing
- create() <- It should never to be used. It is very hard to be right. Here are some alternatives:
    - Observable.fromCallable(() -> value);
        - Whenever someone subscribes to this, return what is inside of the parenthesis, which is () -> value, which returns value
    - Observable.defer(() -> Observable.just(value));
        - Instead of returning a single value, it can return any sort of variables
    - As behaviour subject
    BahviorSubject<String> value;
    Observable<String> value() {
        return value.asObservable();
    }

    void setValue(String s) {
        value.onNext(s);
    }
        - Use RxRelay instead!!

Operators
- Map: Take items of one type and does something to it
- FlatMap: It takes a single item, converts it into a stream of items, and flat it to a single stream











No comments:

Post a Comment