java - RxJava - Non Deterministic behaviour when calling Observables methods -


i have problem following code:

public void foo(list<player> players_list) {      jsonarray playersarr = new jsonarray();      rx.observable.from(players_list) // assume players_list list contains 2 players     .concatmap(player -> {      return getuser(player.getuserid()) // entity 'user' db     .flatmap(userobj -> {         user user = ...;          playersarr.add(new jsonobject()             .putstring("uid", player.getextuserid())         );              return rx.observable.just(playersarr);         });     }).subscribe(playersobj -> {      }, (exception) -> {         log.error("", exception);      }, () -> {          // @ point expect 'playersarr' consist 2 entries!     }); } 

when running code seems output non-deterministic, meaning - of times valid results of 2 entries in jsonarray, 1 entry.

i'm trying figure out why??

edit:
tried switching .flatmap --> .concatmap , seems solve problem im not sure it's solution.

i asume playerarrs not thread safe, if observable executed in asynchronous context, maybe playerarrs may skip add call.

your observable have side effect, has update object exist outside observable. avoid this, can update code in way observable build jsonarray

public void foo(list<player> players_list) {      rx.observable.from(players_list) // assume players_list list contains 2 players                  .concatmap(player -> getuser(player.getuserid()) // entity 'user' db                  .map(userobj -> new jsonobject().putstring("uid", player.getextuserid())                 .reduce(new jsonarray(), (seed, acu) -> seed.add(acu))) // build jsonarray                 .subscribe(playersobj -> { },                             (exception) -> { log.error("", exception); },                            () -> {     // @ point expect 'playersarr' consist 2 entries!                  }); } 

i'm not sure of code, should close need, think.


Comments

Popular posts from this blog

php - failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request -

java - How to filter a backspace keyboard input -

java - Show Soft Keyboard when EditText Appears -