Hello everyone, I've posted a question on stackove...
# rx
h
Hello everyone, I've posted a question on stackoverflow around Flowable, Rx, Kotlin, Retrofit2. Thanks to take a look if you can πŸ‘ https://stackoverflow.com/questions/45441074/multiple-retrofit2-requests-using-flowable-in-kotlin
k
this is how i would solve it:
Copy code
Flowable.zip<User, Comments, Pair<User, Comments>>(
      postService.getUser(postId),
      postService.getCommentsByPostId(postId),
      BiFunction { user, comments -> Pair(user, comments) })
      .subscribeOn(<http://Schedulers.io|Schedulers.io>())
      .observeOn(AndroidSchedulers.mainThread())
      .bindToLifecycle(this)
      .map { (first, second) -> Triple(first, second, ExtraDatas()) }
      .subscribe({
        Log.d("MainActivity", "OnNext")
      }, {
        Log.d("MainActivity", "OnError")
      }, {
        Log.d("MainActivity", "OnComplete")
      })
Use the
zip
or
zipWith
functions to achieve your goal if the retrofit2 calls dont depent on each other. You can find out more here: http://reactivex.io/documentation/operators/zip.html after that map the data from the server with the mainActivity data together. Thats easy as well and finally Kotlin has a very beautiful syntax for labdas so i would encourage you to use them with the specific subscribe function: http://reactivex.io/RxJava/javadoc/io/reactivex/Flowable.html#subscribe(io.reactivex.functions.Consumer,%20io.reactivex.functions.Consumer,%20io.reactivex.functions.Action)
πŸ‘ 1
Also very important to note that i did not use only the raw Rxjava2 lib. i used the libs below: RxAndroid
<https://github.com/ReactiveX/RxAndroid>
for
observeOn(AndroidSchedulers.mainThread())
to get the mainThread. This is because i saw your example and you manipulated the UI without specifing the thread you subscribed on. With this you can achieve that your subscription will be handled on the mainThread RxLifecycle
<https://github.com/trello/RxLifecycle>
for
.bindToLifecycle(this)
this will make sure you dont leave memoryleak if the activity is closed but your retrofit2 call did not finished
πŸ‘ 1
h
A big thank you @kioba for your help πŸ™
πŸ‘ 1
Hope that one day I'll be able to do this by myself 😭 πŸ˜„
So I've used what you've suggest to me and I've modify it a little bit, look:
Copy code
Flowable.zip<User, List<Comment>, Pair<User, List<Comment>>>(
        postService.getUser(post.id),
        postService.getCommentsByPostId(post.id),
        BiFunction { user, comments -> Pair(user, comments) })
        .subscribeOn(<http://Schedulers.io|Schedulers.io>())
        .observeOn(AndroidSchedulers.mainThread())
        .map { (first, second) -> Pair(first, second) }
        .subscribe({
            Log.d("MainActivity", "OnNext")
            userTextView.text = it.first.name
            title.text = post.title
            body.text = post.body
            number_comments.text = it.second.size.toString()

        }, {
            Log.d("MainActivity", "OnError")
        }, {
            Log.d("MainActivity", "OnComplete")
        })
arf I've tried to reformat the code but it didn't worked ok anyway πŸ˜…
so instead of using Comment I've used List<Comment>
πŸ‘ 1
in order to have the number of comments by doing
it.second.size.toString()
I don't know if it's very elegant..
I've used the `Pair`instead of
triple
because I didn't understand what
ExtraDatas()
could do. Maybe I should find/calculate the number of comments inside this method πŸ€”
k
ExtraData was the β€œpost” variable πŸ™‚ just to combine it with the stream
but thats fine with your solutions as well! btw its very cool that you changed to List its much better that way! πŸ™‚
h
✌️ thanks πŸ˜„
all the thanks to you as well, you save me many hours πŸ‘
I've noticed something weird:
so when u click on the detail activity since this process is done asynchronously the data by default are displayed first and boum the correct data from the network are then displayed. Have you notice that when u use rx?
k
use progressbar in your Activity layout. When the activity starts, just start the progresbar, and hide your view (userTextView, number_comments, title, body) After that add a line into the stream:
Copy code
...
.doAfterTerminate { 
 prgoressbar.gone()
userTextView.visible()
...
 }
...
doAfterTerminate will execute after the observable finished with Complete or Error.
πŸ‘ 1
h
yeah this is what usually do with callback I hide/ display the progressbar. Cool thanksπŸ‘
Could you add the solution that you've previously suggested to the stackoverflow question? like this I will valid it. If u don't have time I'll add it no worries πŸ˜‰
I've noticed that sometimes my first rx "request" that look for posts didn't go to onComplete, sometimes and it remains blocked on onNext. I've add what you've suggested previously
.observeOn(AndroidSchedulers.mainThread())
and boum no more pb..πŸ‘
k
good idea! i will answare your stackowerflow as well. πŸ™‚ thanks for the suggestion. about the mainThread, threading is tricky sometimes, but i had no idea this will solve the problem, but now its clear as well πŸ‘
πŸ‘ 1