https://kotlinlang.org logo
#android
Title
# android
s

Shashank Gupta

11/02/2018, 9:10 AM
Hi Guys, I have a query regarding multi-threading in Kotlin. I have 3 streams and am combining them like this, A.withLatestFrom(B, C, { a, b, c -> }).subscribe { } where, A, B, C are PublishSubjects Operations perform inside subscribe { } are: - Accessing Database and fetch data - Process that data and create the list - This list is then pushed to Adapter to display the list Need: I want to run the operation in 2nd thread and finally push the list to main Thread Solutions tried: - used subscribeOn(Schedulers.computation) and observeOn(AndroidSchedulers.mainThread) - used hide() Please help!!!
e

edwardwongtl

11/02/2018, 9:30 AM
I think this should work
Copy code
A.withLatestFrom(B, C) { a, b, c -> ... }
.observeOn(<http://Schedulers.io|Schedulers.io>())
.flatMap { // access DB }
.map { // Process data into list }
.observeOn(AndroidSchedulers.mainThread)
.subscribe { // Push to adapter }
s

Shashank Gupta

11/02/2018, 10:19 AM
@edwardwongtl thanks for suggesting this. Am working over it. Will come back soon with the result. Thanks a lot !
@edwardwongtl it worked. Thanks a lot.
I have a query: why subscribeOn() did not work for PublishSubject ?
@edwardwongtl ^^
e

edwardwongtl

11/02/2018, 12:38 PM
Can you show what you originally wrote?
4 Views