How to process first n items and remaining one differently in a observable stream

南楼画角 提交于 2019-12-19 11:34:22

问题


for example,

given a stream of a certain number (m) of numbers (m1, m2, m3, m4, m5, m6...), and apply a transformation (2 * i) to first n items (n can be less, equal or larger than m), apply another transformation (3 * i) to the remaining items. and

return result : m1*2, m2*2, m3*3, m4*3, m5*3, m6*3... (assuming n=2 here).

I was trying to use take(n) and skip(n) and then concatwith, but looks like take(n) will drop the remaining items in the sequence , and make skip(n) after that return nothing.


回答1:


You can share your m's stream, and then merge back together take() and skip() streams, something like this:

    int m = 10;
    int n = 8;
    Observable<Integer> numbersStream = Observable.just(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
            .publish();

    Observable<Integer> firstNItemsStream = numbersStream.take(n)
            .map(i -> i * 2);

    Observable<Integer> remainingItemsStream = numbersStream.skip(n)
            .map(i -> i * 3);

    Observable.merge(firstNItemsStream, remainingItemsStream)
            .subscribe(integer -> System.out.println("result = " + integer));
    numbersStream.connect();

EDIT:
As point out by @A.E. Daphne, share() will start emitting with the first subscriber, thus second subscriber might miss notification/s if the Observable started already to emit item/s, so in this case there are other possibilities:
cache() - will reply all cache emitted items and reply them to each new subscriber, but will sacrifice the unsubscription ability, thus need to be used carefully.
reply().refCount() - will create Observable that reply() all previous items to each new Subscriber (similar to cache), but will unsubscribe when the last subscriber unsubscribe from it.

In both cases, memory should take into consideration as the Observable will cache all emitted items in memory.

publish() - Additional possibility, without caching all previous items, would be to use publish() to create ConnectableObservable, and call it's connect() method to begin emissions after all required subscribers subscribed, thus will get synchronization and all subscribers will get all notifications correctly.



来源:https://stackoverflow.com/questions/42826259/how-to-process-first-n-items-and-remaining-one-differently-in-a-observable-strea

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!