티스토리 뷰
Operator
데이터 스트림을 쉽게 변형하기 위해 Operator를 사용합니다. Collections에서 제공하는 forEach, map 같은 Operator뿐만 아니라 Rx에서 추가적으로 제공하는 debounce, buffer 같은 함수들이 있습니다.
https://reactivex.io/documentation/operators.html
ReactiveX - Operators
Introduction Each language-specific implementation of ReactiveX implements a set of operators. Although there is much overlap between implementations, there are also some operators that are only implemented in certain implementations. Also, each implementa
reactivex.io
debounce
이벤트가 빠르게 여러번 발생한 경우 마지막 이벤트만 사용하고 싶을 때 유용합니다. 아래 예시에서 (2, 3, 4, 5)가 빠르게 입력된 경우 5만 발행하는 것을 확인할 수 있습니다.

만약 유저에게 입력을 받고 검색하는 경우 debounce를 사용하지 않으면 모든 입력값을 검색하게 됩니다.
fun notDebounce() {
val userInput = listOf("ㄱ", "가", "강", "강ㅌ", "강태", "강탲", "강태조", "강태종")
Observable.create<String> { emitter ->
userInput.forEach {
Thread.sleep(Random.nextLong(50L..200L))
emitter.onNext(it)
}
}.subscribe {
println(it)
}
Thread.sleep(2000L)
}
ㄱ
가
강
강ㅌ
강태
강탲
강태조
강태종
debounce를 사용하게 되면 입력 이벤트를 적절하게 발행할 수 있습니다. 첫번째 입력을 기준으로 150ms 만큼 입력을 받고 그 구간중 제일 마지막에 입력한 데이터를 발행합니다.
fun debounce() {
val userInput = listOf("ㄱ", "가", "강", "강ㅌ", "강태", "강탲", "강태조", "강태종")
Observable.create<String> { emitter ->
userInput.forEach {
Thread.sleep(Random.nextLong(50L..200L))
emitter.onNext(it)
}
}.debounce(
150L, TimeUnit.MILLISECONDS
).subscribe {
println(it)
}
Thread.sleep(2000L)
}
ㄱ
강ㅌ
강태종
buffer
buffer는 데이터를 발행하는 속도와 옵저버가 데이터를 받고 처리하는 속도 사이의 차이가 클 때 사용할 수 있습니다. 발행하는 데이터를 지정된 사이즈만큼 하나의 버퍼로 저장하여 발행합니다.

fun buffer() {
Observable.just(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
.buffer(3)
.subscribe {
println(it)
}
}
[1, 2, 3]
[4, 5, 6]
[7, 8, 9]
[10]
map
Collections Operation의 map과 유사합니다. 발행되는 데이터를 다른 형태로 매핑합니다.

fun map() {
Observable.just(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
.map {
it * 10
}
.subscribe {
println(it)
}
}
10
20
30
40
50
60
70
80
90
100'Android > RxJava' 카테고리의 다른 글
| Android RxJava - Scheduler (0) | 2022.03.05 |
|---|---|
| Android RxJava - Backpressure (0) | 2022.03.02 |
| Android RxJava - Hot Observable, Cold Observable (0) | 2022.03.02 |
| Android RxJava - Observable (0) | 2022.03.01 |
| Android RxJava - Reactive Programming (0) | 2022.02.28 |
- Total
- Today
- Yesterday
- clean code
- CancellationException
- 보이스카우트 규칙
- git
- Flutter
- ViewModelProvider
- Flowable
- viewmodel
- ConcatAdapter.Config
- isActive
- Coroutine
- 함수
- Exception
- Kotlin
- 클린 코드
- Android
- ConcatAdapter
- 연산자
- observable
- TDD
- rxjava
- ViewModelStoreOwner
- gradle
- 클린코드
- DART
- null
- commit
- 코루틴
- Widget
- DSL
| 일 | 월 | 화 | 수 | 목 | 금 | 토 |
|---|---|---|---|---|---|---|
| 1 | 2 | 3 | 4 | 5 | 6 | 7 |
| 8 | 9 | 10 | 11 | 12 | 13 | 14 |
| 15 | 16 | 17 | 18 | 19 | 20 | 21 |
| 22 | 23 | 24 | 25 | 26 | 27 | 28 |
