Blog Infos
Author
Published
Topics
,
Published
RxJava is not only Single
fun uploadData(): Observable<UpdateStep> = Observable.create<UpdateStep> {
it.onNext(UpdateStep.Step1)
doUpdateStep1()
it.onNext(UpdateStep.Step2)
doUpdateStep2()
it.onNext(UpdateStep.Step3)
doUpdateStep3()
it.onNext(UpdateStep.Success)
}.onErrorReturn {
UpdateStep.Error(it)
}

Using RxJava

fun uploadData(): Flow<UpdateStep> = flow {
emit(UpdateStep.Step1)
doUpdateStep1()
emit(UpdateStep.Step2)
doUpdateStep2()
emit(UpdateStep.Step3)
doUpdateStep3()
emit(UpdateStep.Success)
}.catch {
emit(UpdateStep.Error(it))
}

And using Flow

 

You’re hot then you’re cold
class BleManager : BleConnectionObserver {
private val _bleConnectionStateSubject = BehaviorSubject.createDefault(ConnectionState.Disconnected)
val bleConnectionStateObservable: Observable<ConnectionState> = _bleConnectionStateSubject.hide()
val currentConnectionState: ConnectionState
get() = _bleConnectionStateSubject.value
override fun onDeviceConnected(device: BluetoothDevice) {
/* ... */
_bleConnectionStateSubject.onNext(ConnectionState.Connected)
}
override fun onDeviceDisconnected() {
/* ... */
_bleConnectionStateSubject.onNext(ConnectionState.Disconnected)
}
}
class BleManager : BleConnectionObserver {
private val _bleConnectionStateFlow = MutableStateFlow(ConnectionState.Disconnected)
val bleConnectionStateFlow: StateFlow<ConnectionState> = _bleConnectionStateFlow
override fun onDeviceConnected(device: BluetoothDevice) {
/* ... */
_bleConnectionStateFlow.value = ConnectionState.Connected
}
override fun onDeviceDisconnected() {
/* ... */
_bleConnectionStateFlow.value = ConnectionState.Disconnected
}
}

Job Offers

Job Offers


    Android Engineer

    American Express
    Phoenix, USA
    • Full Time
    apply now

    Senior Android Developer (Remote)

    Komoot
    Europe
    • Full Time
    apply now

    Android Engineering Manager

    Peloton
    New York, USA
    • Full Time
    apply now
Load more listings

OUR VIDEO RECOMMENDATION

, ,

Migrating to Kotlin State & Shared Flows

State Flows and Shared Flows were introduced to broadcast events to multiple consumers with coroutines. You might have been using Broadcast Channels and are starting to migrate to Shared Flows. Or, you might be converting…
Watch Video

Migrating to Kotlin State & Shared Flows

Mohit Sarveiya
Google Developers Expert Kotlin & Android
Google

Migrating to Kotlin State & Shared Flows

Mohit Sarveiya
Google Developers Ex ...
Google

Migrating to Kotlin State & Shared Flows

Mohit Sarveiya
Google Developers Expert ...
Google

Jobs

Ok, cool, but do we even need that?

YOU MAY BE INTERESTED IN

YOU MAY BE INTERESTED IN

blog
It’s one of the common UX across apps to provide swipe to dismiss so…
READ MORE
blog
Hi, today I come to you with a quick tip on how to update…
READ MORE
blog
Automation is a key point of Software Testing once it make possible to reproduce…
READ MORE
blog
Drag and Drop reordering in Recyclerview can be achieved with ItemTouchHelper (checkout implementation reference).…
READ MORE

Leave a Reply

Your email address will not be published.

Fill out this field
Fill out this field
Please enter a valid email address.

Menu