Reputation:
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
Upvotes: 0
Views: 1255
Reputation: 3698
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 ;
}
Upvotes: 1