RxJava : observable emitting duplicate values

user7154703

I am using observables with arraylist of 3 elements "one","two" and "three". Am testing them in 2 similar but slightly different manner. You may consider them as section 1 and section 2 in below code.

Result of each test supposed to return 3 line through sys out. It does so efficiently if I keep only one section there and comment out other section.

However am not able to understand , if both sections are kept there, section 2, instead of returning 3 values will return 6 values.

Somehow it is influenced by previous run (section 1), but not sure How. I tried to unsubscribe first observable , but it didn't resolve the issue.

Please check, if you can find out what exactly I am missing in this code.

        import rx.Observable;
        import rx.Subscription;
        import rx.functions.Func1;

        import java.util.ArrayList;
        import java.util.List;


        public class RxJavaTest {

               static List<String> list = new ArrayList()  ;

            static Observable<List<String>> query(String text)
            {
                list.add("one") ;list.add("two") ;list.add("three") ;

                Observable<List<String>> results = Observable.just(list) ;

                return  results ;
            }

            public static void main(String[] args) {

                Subscription subscription ;

                subscription = query("Hello, World 1").subscribe(urls -> {
                    Observable.from(urls).subscribe(url -> System.out.println("section 1 "+url)) ;
                }) ;

                subscription.unsubscribe();

                query("Hello, World 2")
                        .subscribe(urls -> {
                            for (String url : urls) {
                                System.out.println(" section 2  "+url);
                            }
                        } ) ;


            }

        }

Sys Out Logs : marked which records are unexpected , if you remove code of section 1, it will not be there

section 1 one
section 1 two
section 1 three

 section 2  one
 section 2  two
 section 2  three
 section 2  one --duplicate
 section 2  two --duplicate
 section 2  three --duplicate
Artur Biesiadowski

You have static list. You are adding to it twice (one in each call to query), so during second run, you have double number of elements.

Instead of

        static List<String> list = new ArrayList()  ;

        static Observable<List<String>> query(String text)
        {
            list.add("one") ;list.add("two") ;list.add("three") ;

            Observable<List<String>> results = Observable.just(list) ;

            return  results ;
        }

do something like

        static Observable<List<String>> query(String text)
        {
            List<String> list = new ArrayList()  ;
            list.add("one") ;list.add("two") ;list.add("three") ;
            Observable<List<String>> results = Observable.just(list) ;
            return  results ;
        }

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Identify emitting observable in RxJava

Observable not emitting any values

How to filter duplicate values emitted by observable in RXJava?

In RxJava, is emitting event by observable multi-threaded?

RxJava, combining two observables and emitting second observable

rxjs pairwise is emitting duplicate values

RxJava Observable.create not emitting events for switchIfEmpty(observable)

RxJava2 connectable observable - replay not emitting all previous items

Using RxJava for email login validation, an observable is emitting twice

'Incompatible type' error in emitting a String array in an Observable in RxJava 2

RxJs Observable duplicate values

RxJava: prevent an observable from emitting until data from another observable is emitted

Does an observable keep emitting values after observer has unsubscribed?

How to get notification when the last observable finish emitting values?

RxJava onBackpressureBuffer not emitting items

RxJava BehaviorSubject not emitting last item?

Observable only emitting first value

Return observable before emitting event

RxJS forkJoin not emitting values

dynamic interval after emitting item in rxjava

Understanding RxJava observable when underlying data source has new values

RxJava - two Observable sources, combine output only on certain values

Emitting signal not updating the values in GUI

Emitting a hot boolean observable using switchMap?

combineLatest is not emitting when the second observable changes?

RxJS - Observable is not emitting value with .next() on first call

RxJs Observable - Handle 404 by emitting default value

Piped observable not emitting when source emits

Filter RxJava Observable with other Observable