Android Live Objects/Collections
Live Objects are supported in the Android SDK with RxJava Data Streaming
The RxJava2 library is being used in Android development to achieve Live Object 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.
SDK handles lots of data received from various sources. Data can be present in local cache. It might also be queried from the server or received from some real-time events. What this means is that same data is constantly updating. The data that you are accessing at the moment can get updated by other sources and becomes out of sync.
Rx2 Data Stream helps in syncing the data so you will always get the most recent one. Whenever the data updates, you will be notified through Flowable Objects and Flowable Collection.
New data gets automatically collected everytime when there is an updation and user need not refresh to get the recent data.
Live Collection is available for the following functionalities in user/community feeds:
- Post collection
- Reactions collection
- Followers/Following collection
To retrieve data from the RxStream, we need to subscribe to the Stream(Flowable/Single/Completable) by defining subscribing and observing threads.
In the RxJava2 framework we have these different types of objects that can be observed:
- 1.Flowable - emits a stream of elements
doOnNext
doOnError
- 2.Single - emits exactly one element
doOnSuccess
doOnError
- 3.Completable - emits a “complete” event, without emitting any data type, just a success/failure
doOnComplete
doOnError
To get channel data, below is a sample code.
Since all of the SDK functions are returned in Rx2 form, converting Flowable to Kolin Flow or Android LiveData can be done easily with few additional steps.
To Convert Flowable to Flow - Kotlin provided the convinient function
asFlow
to convert Flowable. This can be done by including the depdency in your project. Please refer here for further information. implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-rx2:x.y.z'
Here's the example of conversion function from Flowable to Flow by using
asFlow
AmityChatClient.newChannelRepository()
.getChannel(channelId)
.asFlow()
.catch { exception: Throwable -> }
.collect { channel: AmityChannel -> }
To convert Flowable to LiveData - Android KTX provided the convinient function
toLiveData
to convert Flowable. This can be done by including the depdency in your project. Please refer here for further information. implementation 'androidx.lifecycle:lifecycle-reactivestreams-ktx::x.y.z'
Here's the example of conversion function from Flowable to LuveData by using
toLiveData
AmityChatClient.newChannelRepository()
.getChannel(channelId)
.toLiveData()
.observe(this) { channel: AmityChannel ->
}
Last modified 1mo ago