Created
March 4, 2020 11:08
-
-
Save addam01/3423554f74293f87133c915cfbb08d85 to your computer and use it in GitHub Desktop.
RxJava RxKotlin for iterating a list of observables
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class RxJavaIterate{ | |
val users: List<User> = listOf() | |
//We want to iterate each of these users in the list and perform a function individually in each items | |
//One By One | |
Observable.from(users) | |
.map(i -> { | |
//transform users to username string | |
//save each i into db | |
}) | |
.subscribeOn(Schedulers.io()) | |
.observeOn(AndroidSchedulers.mainThread()) | |
.subscribe(item ->{ | |
} | |
// If we already have an observables that emits a list | |
fun listObserve(someval: List<User>) : Observable<List<User>{ | |
return Observable.just(someval) | |
} | |
listObserve.flatMap(Observable::from) | |
.subscribe(item -> { | |
}) | |
// If we wanna transform all values in one place but emit them one by one on separate thread | |
Observable.create(subscriber ->{ | |
for(user: User in List<User>){ | |
subscriber.onNext(doSomethingFunction(user)) | |
} | |
subscriber.onCompleted() | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment