1

I have a service like this

 @GET("/")
fun getInfo(@Query("pew") pew1: Double, @Query("pew2") pew2: Double): Observable<PewResponse>

So now, when I have this

I have a List to fill with server data

private var mPewList: MutableList<PewList> = arrayListOf()

And then I have this function

override fun getResultFromNetwork(): Observable<PewResponse> {
    val pewObservable = pewService.getInfo("pew1","pew2")
    return pewObservable.concatMap {
      //How do I fill my list ??

    }


}

I'm pretty new to this rxJava so I saw people using Single and other guys using Observable what's better?

EDIT

I'm following this example https://github.com/joanby/android-mvp/blob/master/MoviesFeed/app/src/main/java/com/juangabriel/moviesfeed/movies/MoviesRepository.java

And now I want to do something like this :

@Override
public Observable<Result> getResultFromNetwork() {

    Observable<TopMoviesRated> topMoviesRatedObservable = moviesApiService.getTopMoviesRated(1)
            /*.concatWith(moviesApiService.getTopMoviesRated(2))
            .concatWith(moviesApiService.getTopMoviesRated(3))*/;

    return topMoviesRatedObservable
            .concatMap(new Function<TopMoviesRated, Observable<Result>>() {
                @Override
                public Observable<Result> apply(TopMoviesRated topMoviesRated) {
                    return Observable.fromIterable(topMoviesRated.getResults());
                }
            }).doOnNext(new Consumer<Result>() {
                @Override
                public void accept(Result result) {
                    results.add(result);
                }
            });

}

So as I'm seeing he fills the result list, and then on presenter do this : https://github.com/joanby/android-mvp/blob/master/MoviesFeed/app/src/main/java/com/juangabriel/moviesfeed/movies/MoviesPresenter.java

StuartDTO
  • 783
  • 7
  • 26
  • 72
  • I don't understand how or why you want `mPewList` filled. Also why `concatMap` which is used to flatten a stream of observables of X into a stream of X? Is PewResponse observable? If not, you'll most likely want just `map` ("transform the items emitted by an Observable by applying a function to each item") I guess? – zapl Oct 21 '18 at 22:38
  • @zapl Please, see my edit, maybe it's more understandable now... sorry – StuartDTO Oct 21 '18 at 22:56
  • Hm, that code is shady as well. `topMoviesRatedObservable.flatMapIterable(top -> top.getResults())` should do the same and make more sense than `concatMap`. And storing things in a `results` list sounds like a bad idea because it's probably not threadsafe. – zapl Oct 21 '18 at 23:22
  • @zapl So what should be a correct way? could you provide an answer following that github please? – StuartDTO Oct 22 '18 at 06:11
  • @zapl Could you provide an alternative to this? Please I really need this – StuartDTO Oct 22 '18 at 07:18
  • For the result of `getInfo` it probably makes sense to use `Single` as explained in https://stackoverflow.com/q/41982370/995891 but otherwise, like p.alexey says, the logic in the very end shouln't be more than a) `.observeOn(AndroidSchedulers.mainThread())` to have threadsafe access to the UI and then b) `.subscribe({ pewResponse -> mPewList = pewResponse./* something*/` to handle the result once available. Maybe `.subscribeWith(DisposableObserver(.. ` to get rid of the observable (e.g. https://medium.com/@elye.project/kotlin-and-retrofit-2-tutorial-with-working-codes-333a4422a890) – zapl Oct 22 '18 at 09:51

1 Answers1

2

If your data from server is monolithic you should to use Single because it return only once value. The Observable will do when your data arrives in chunks.

For more details you can see this answer: https://stackoverflow.com/a/42759432/9060113

EDIT:

@GET("/")
fun getInfo(@Query("pew") pew1: Double, @Query("pew2") pew2: Double): Single<PewResponse>


override fun getResultFromNetwork() {
  pewService
     .getInfo("pew1","pew2")
     .subscribeOn(Schedulers.io()) // for background processing
     .observeOn(AndroidSchedulers.mainThread()) // get result in Main Thread (it require rxAndroid lib)
     .subscribe(
       { pewResponse -> // parse response and fill list },
       { // handle error }
     )
}
punchman
  • 1,350
  • 1
  • 13
  • 23
  • Ok, it's nice explained, but how do i fill my data? – StuartDTO Oct 21 '18 at 22:30
  • I edited answer. You need to subscribe on your Single/Observable and parse you answer -> fill list. – punchman Oct 21 '18 at 22:39
  • But what I'm doing on my PresenterImpl is this, right? Because I need to do something like this, see my edit – StuartDTO Oct 21 '18 at 22:53
  • Author from gitHub uses `concatMap` only for transform each `TopMoviesRated` to `List`. And then (in `MoviesModel`) he transform result `List` into ViewModel. Then presenter get this Observable, subscribes on it and work with each ViewModel separately (in this case Observable is necessarily). – punchman Oct 21 '18 at 23:22
  • In my opinion this approach is very entangled. The data conversion logic is distributed across multiple entities (Repository and MoviesModel). I think that the data preparation logic should be concentrated in the Repository (or Interactor, if you use it). And your Presenter/Interactor already interacts this Repository and subscribes to Observable. Then in `onNext` callback your presenter gets piece of data and sent it to View for display. – punchman Oct 22 '18 at 07:24
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/182245/discussion-between-stuartdto-and-p-alexey). – StuartDTO Oct 22 '18 at 07:44
  • See the chat please – StuartDTO Oct 22 '18 at 09:13