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

前端 未结 1 1462
青春惊慌失措
青春惊慌失措 2021-01-07 11:26

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, equ

相关标签:
1条回答
  • 2021-01-07 11:54

    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.

    0 讨论(0)
提交回复
热议问题