RXJava is a reactive programming library for implementing easy asynchronous code by using Observer pattern, Iterator pattern and functional programming. I'm currently using it in some projects and intent keep this repo updated as I start using any other feature by this lib.
implementation "io.reactivex.rxjava3:rxjava:3.1.5"
Just - Observable.just()
Convert an object or a set of objects into an Observable. Check
Just().sample()
π
Observable.just(1, "Two", false, Date()).subscribe { Log.d(TAG, "onNext: $it") }
//onNext: 1
//onNext: Two
//onNext: false
//onNext: Wed Jul 27 15:38:35 GMT-03:00 2022
Create - Observable.create()
Will create a fresh instance of ObservableEmitter every time a subscriber start listening to the scope implementation result.. Check
Create().sample()
π
Observable.create {
try {
for (i in 1..5) {
it.onNext(i)
}
it.onComplete()
} catch (e: Exception) {
it.onError(e)
}
}.subscribe({
Log.d(TAG, "onNext: $it")
}, {
Log.d(TAG, "onError $it")
}, {
Log.d(TAG, "onComplete")
})
//onNext: 1
//onNext: 2
//onNext: 3
//onNext: 4
//onNext: 5
//onComplete
FromArray - Observable.fromArray()
Converts an array into an Observable that emits those items. Check
FromArray().sample()
π
Observable.fromArray("First", "Second", "Third").subscribe { Log.d(TAG, "onNext: $it") }
//onNext: First
//onNext: Second
//onNext: Third
FromIterable - Observable.fromIterable()
Converts an collection into an Observable that emits the items as sequence. Check
FromIterable().sample()
π
Observable.fromIterable(listOf("One", "Two", "Three")).subscribe { Log.d(TAG, "onNext: $it") }
//onNext: One
//onNext: Two
//onNext: Three
Range - Observable.range()
Returns an Observable that emits a sequence of Integers within a specified range. Check
Range().sample()
π
Observable.range(5, 3).subscribe { Log.d(TAG, "onNext: $it") }
// onNext: 5
// onNext: 6
// onNext: 7
Repeat - Observable.repeat()
Returns an Observable that repeats the sequence of items emitted by the current Observable at most count times. Check
Repeat().sample()
π
Observable.just("My Text").repeat(2).subscribe { Log.d(TAG, "onNext: $it") }
//onNext: My Text
//onNext: My Text
Interval - Observable.interval()
Emits a sequential number every specified interval of time. On Android devices works even in background. Check
Interval().sample()
π
Observable.interval(1, TimeUnit.SECONDS).subscribe { Log.d(TAG, "onNext: Hit") }
//onNext: Hit
//onNext: Hit
//onNext: Hit
//onNext: Hit
//onNext: Hit
//onNext: Hit
//... and continues
Timer - Observable.timer()
Emits one single time after a specified delay. Check
Timer().sample()
π
Observable.timer(2, TimeUnit.SECONDS).subscribe { Log.d(TAG, "onNext: delayed by 2 seconds") }
//onNext: delayed by 2 seconds
Filter - Observable.filter()
Filters items emitted by the current Observable. Check
Filter().sample()
π
Observable
.fromIterable(listOf("Home", "Horse", "Castle", "Game"))
.filter { it.startsWith("H") }
.subscribe {
Log.d(TAG, "onNext: $it")
}
//onNext: Home
//onNext: Horse
First - Observable.firstElement()
Emits only the very first item or empty using a Maybe interface. See also Observable.firstOrError(). Check
First().sample()
π
Observable
.fromArray("Yellow", "Blue", "Green", "Black")
.firstElement()
.subscribe {
Log.d(TAG, "onNext: $it")
}
//onNext: Yellow
Last - Observable.lastElement()
Emits only the very last item or empty using a Maybe interface. See also Observable.lastOrError(). Check
Last().sample()
π
Observable
.fromArray("Yellow", "Blue", "Green", "Black")
.lastElement()
.subscribe {
Log.d(TAG, "onNext: $it")
}
//onNext: Black
Distinct - Observable.distinct()
Emits all items that are distinct based on Object.equals(Object) comparison. Check
Distinct().sample()
π
Observable.fromArray("Yellow", "Blue", "Green", "Black", "Yellow", "Blue")
.distinct()
.subscribe {
Log.d(TAG, "onNext: $it")
}
//onNext: Yellow
//onNext: Blue
//onNext: Green
//onNext: Black
Skip - Observable.skip()
Skips the first count items emitted by the current Observable and emits the remains. Check
Skip().sample()
π
Observable.just(1, 2, 3, 4, 5, 6, 7)
.skip(5)
.subscribe {
Log.d(TAG, "onNext: $it")
}
//onNext: 6
//onNext: 7
SkipLast - Observable.skipLast()
Drops a specified number of items from the end of the sequence emitted by the current Observable. Check
SkipLast().sample()
π
Observable.just(1, 2, 3, 4, 5, 6, 7)
.skipLast(5)
.subscribe {
Log.d(TAG, "onNext: $it")
}
//onNext: 1
//onNext: 2
TakeWhile - Observable.takeWhile()
While the condition is satisfied, emits the items by the Observable. Check
TakeWhile().sample()
π
Observable
.interval(1, TimeUnit.SECONDS)
.takeWhile { it <= 3 }
.subscribe {
Log.d(TAG, "onNext: $it - Hit")
}
//onNext: 0 - Hit
//onNext: 1 - Hit
//onNext: 2 - Hit
//onNext: 3 - Hit
Buffer - Observable.buffer()
Emits buffers of items it collects from the current Observable, based on count, timing or boundary defined. Check
Buffer().sample()
π
Observable.just("Mother", "Father", "Daughter", "Son")
.buffer(2)
.subscribe {
Log.d(TAG, "onNext: $it")
}
//onNext: [Mother, Father]
//onNext: [Daughter, Son]
Map - Observable.map()
Applies a specified function to each item to transform the results of these emit values. Check
Map().sample()
π
Observable.fromArray(2, 4, 2)
.map { "#".repeat(it) }
.subscribe {
Log.d(TAG, "onNext: $it")
}
//onNext: ##
//onNext: ####
//onNext: ##
- Github Repo @github.com/ReactiveX/RxJava
- Documentation @reactivex.io/documentation
- Course I - @udemy.com/course/rxjava-rxandroid-i
- Course II - @udemy.com/course/rxjava-rxandroid-ii