ReactiveX
RxAndroid
smomo
2021. 5. 2. 16:01
RxJava에 최소한의 클래스를 추가하여 Android Application에서 쉽고 간편하게 사용하게 만든 라이브러리이다.
안드로이드에서 사용할 수 있는 Reactive API 및 라이브러리 목록은
https://github.com/Reactivex/Rxandroid/wiki 에서 확인할 수 있다.
환경 설정
allprojects {
repositories {
maven { url "https://oss.jfrog.org/libs-snapshot" }
}
}
dependencies {
implementation 'io.reactivex.rxjava3:rxandroid:3.0.0'
// Because RxAndroid releases are few and far between, it is recommended you also
// explicitly depend on RxJava's latest version for bug fixes and new features.
// (see https://github.com/ReactiveX/RxJava/releases for latest 3.x.x version)
implementation 'io.reactivex.rxjava3:rxjava:3.0.0'
}
Observing on the main thread
Observable.just("one", "two", "three", "four", "five")
.subscribeOn(Schedulers.newThread())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(/* an Observer */);
새 스레드에서 Observable을 실행하고 메인 스레드에서 onNext를 통해 결과를 내보낸다.
Observing on arbitrary loopers
임의의 Looper에서 Observable을 관찰하려면 AndroidSchedulers.from을 호출하여 연관된 스케줄러를 만든다.
Looper backgroundLooper = // ...
Observable.just("one", "two", "three", "four", "five")
.observeOn(AndroidSchedulers.from(backgroundLooper))
.subscribe(/* an Observer */)
새 스레드에서 Observable을 실행하고 backgroundLooper를 실행하는 스레드에서 onNext를 통해 결과를 내보낸다.
References
- https://reactivex.io/
- https://github.com/Reactivex/Rxandroid
- 유동환, 박정준, 리액티브 프로그래밍 기초부터 안드로이드까지 한번에, 한빛미디어