RxJava: how to compose multiple Observables with dependencies and collect all results at the end?

后端 未结 3 1465
终归单人心
终归单人心 2021-02-07 03:08

I\'m learning RxJava and, as my first experiment, trying to rewrite the code in the first run() method in this code (cited on Netflix\'s blog as a problem RxJava ca

3条回答
  •  盖世英雄少女心
    2021-02-07 03:52

    It looks like all you really need is a bit more encouragement and perspective on how RX is used. I'd suggest you read more into the documentation as well as marble diagrams (I know they're not always useful). I also suggest looking into the lift() function and operators.

    • The entire point of an observable is to concatenate data flow and data manipulation into a single object
    • The point of calls to map, flatMap and filter are to manipulate the data in your data flow
    • The point of merges are to combine data flows
    • The point of operators are to allow you to disrupt a steady stream of observables and define your own operations on a data flow. For example, I coded a moving average operator. That sums up n doubles in an Observable of doubles to return a stream of moving averages. The code literally looked like this

      Observable movingAverage = Observable.from(mDoublesArray).lift(new MovingAverageOperator(frameSize))

    You'll be a relieved that a lot of the filtering methods that you take for granted all have lift() under the hood.

    With that said; all it takes to merge multiple dependencies is:

    • changing all incoming data to a standard data type using map or flatMap
    • merging standard data-types to a stream
    • using custom operators if one object needs to wait on another, or if you need to order data in the stream. Caution: this approach will slow the stream down
    • using to list or subscribe to collect all of that data

提交回复
热议问题