Data-Stream
The RxJava2 library is being used in Android development to achieve LiveObject behavior of the other platforms.
It is a Java VM implementation of ReactiveX, a library for composing asynchronous and event-based programs by using observable sequences. The building blocks of RxJava are Observables and Subscribers. Observable is used for emitting items and Subscriber is used for consuming those items.
You can visit ReactiveX for more information.

How to retrieve data from Data Stream

To retrieve data from the RxStream, we need to subscribe to the Stream(Flowable/Single/Completable) by defining subscribing and observing threads.
1
//Flowable
2
val flowableStream: Flowable<T> = … // Flowable initialization
3
4
flowableStream
5
.subscribeOn(Schedulers.io()) // subscribing on io thread(Background thread)
6
.observeOn(AndroidSchedulers.mainThread()) // observing on main thread
7
.doOnNext {
8
// data is available here
9
}.doOnError {
10
// handle error here
11
}.subscribe()
Copied!

Events a Data Stream can emit

In the RxJava2 framework we have these different types of objects that can be observed:
  1. 1.
    Flowable - emits a stream of elements
    • doOnNext
    • doOnError
  2. 2.
    Single - emits exactly one element
    • doOnSuccess
    • doOnError
  3. 3.
    Completable - emits a “complete” event, without emitting any data type, just a success/failure
    • doOnComplete
    • doOnError

Data Stream Interface

Data Stream uses all standard RxJava2 operators.

Get post

To get post data, below is a sample code.
1
AmitySocialClient.newPostRepository().getPost(postId)
2
.subscribeOn(Schedulers.io())
3
.observeOn(AndroidSchedulers.mainThread())
4
.doOnNext { amityPost ->
5
// AmityPost data is available
6
val postId = amityPost.getPostId()
7
}.doOnError {
8
// handle error here
9
}.subscribe()
Copied!
Last modified 2mo ago