Hawazine Haouat
08/01/2017, 4:28 PMkioba
08/02/2017, 9:14 AMFlowable.zip<User, Comments, Pair<User, Comments>>(
postService.getUser(postId),
postService.getCommentsByPostId(postId),
BiFunction { user, comments -> Pair(user, comments) })
.subscribeOn(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)kioba
08/02/2017, 9:23 AMhttps://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 finishedHawazine Haouat
08/03/2017, 8:32 AMHawazine Haouat
08/03/2017, 8:32 AMHawazine Haouat
08/03/2017, 8:33 AMHawazine Haouat
08/03/2017, 8:39 AMFlowable.zip<User, List<Comment>, Pair<User, List<Comment>>>(
postService.getUser(post.id),
postService.getCommentsByPostId(post.id),
BiFunction { user, comments -> Pair(user, comments) })
.subscribeOn(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")
})Hawazine Haouat
08/03/2017, 8:40 AMHawazine Haouat
08/03/2017, 8:40 AMHawazine Haouat
08/03/2017, 8:41 AMit.second.size.toString() I don't know if it's very elegant..Hawazine Haouat
08/03/2017, 8:45 AMtriple because I didn't understand what ExtraDatas() could do. Maybe I should find/calculate the number of comments inside this method π€kioba
08/03/2017, 8:47 AMkioba
08/03/2017, 8:48 AMHawazine Haouat
08/03/2017, 8:51 AMHawazine Haouat
08/03/2017, 8:53 AMHawazine Haouat
08/03/2017, 8:53 AMHawazine Haouat
08/03/2017, 8:55 AMkioba
08/03/2017, 9:09 AM...
.doAfterTerminate {
prgoressbar.gone()
userTextView.visible()
...
}
...kioba
08/03/2017, 9:09 AMHawazine Haouat
08/03/2017, 9:16 AMHawazine Haouat
08/03/2017, 10:41 AMHawazine Haouat
08/03/2017, 10:45 AM.observeOn(AndroidSchedulers.mainThread()) and boum no more pb..πkioba
08/03/2017, 11:43 AM