Managing State with RxJava by Jake Wharton
Why Stack Overflow recommends it
Stack Overflow users link it when asking or answering how to model UI and request state in RxJava, such as loading, success and error events, and why UI work does not belong in side-effect operators.
Recommended by 5 people across 5 different months, 2017 to 2020.
What it is
Jake Wharton's Devoxx 2017 talk on managing application state with RxJava, aimed at people already comfortable with Rx. It looks at state-management approaches from other platforms and languages and how they fit MVP and MVVM style architectures, with a focus on making app state more deterministic.
What people said
Recommends Dariusz Seweryn · 2018-03 · Stack Overflow · on “How to respond to BLE characteristic notifications by sending a new write command”
The best approach, according to this Jake Wharton's talk [this video] would be to construct an Observable that would emit just values that are needed for updating your model. (example in Kotlin) We could have these outputs of the Observable : sealed class ConnectionEvent { object CloseConnection : ConnectionEvent() // dummy event to notify when the connection can be closed data class SerialNumber(val byteArray: ByteArray) : ConnectionEvent() data class BatteryLevel(val byteArray: ByteArray) : ConnectionEvent() data class Answer4(val byteArray: ByteArray) : ConnectionEvent() } And the whole flow could look like this: bleDevice.establishConnection(false) .flatMap { connection -> val batteryLevelSin
Recommends kingston · 2018-04 · Stack Overflow · on “update choosen items price using rxAndroid”
RxJava is complex. If you want to use it you need to study it properly. It can't be explained in an answer on StackOverflow. [this video]
Recommends emenegro · 2018-05 · Stack Overflow · on “RxSwift, use .scan to keep track of the state of an object”
It is all related with statelessness. As I am learning Rx, and Rx has a tight relation with functional paradigm, I prefer to get all the knowledge about this in this first steps. You can learn more about this watching this talk about Reactive State by Jake Wharton, it is very interesting: [this video]
Recommends luis_cortes · 2019-06 · Stack Overflow · on “What is the purpose of doOnNext(...) in RxJava”
I'd like to caution anyone reading this answer from interacting with the UI (showing a progress bar) in `doOnNext`. As the accepted answer mentions, `doOnNext` should be used for side effects. The UI is not a side effect. While you may not run into trouble with a trivial example, treating the UI as a side effect can create some pretty nasty state management issues in a more complex situation. See Managing State with RxJava by Jake Wharton [this video] for more information. Jump to 16:30 for the TL;DR.
Recommends Pawel · 2020-02 · Stack Overflow · on “Sending network request using switchMap android”
I think what you are looking for is to simply disable the button when the request is in progress and enable it when it completes or errors. pseudocode: buttonClickObservable.map(e -> searchTextView.getText().toString()) .doOnNext(e -> buttonClickView.setEnabled(false)) .switchMap(searchText -> sendNetworkRequestToServer(searchText)); .observeOn(AndroidSchedulers.mainThread()) .subscribe( result -> ..., error -> buttonClickView.setEnabled(true), () -> buttonClickView.setEnabled(true) ) However this solution is not ideal. I think what you are looking for is to follow the MVI architecture and manage the state properly. If you are interested this is a great talk to watch: [this video]
Cites urSus · 2017-05 · Stack Overflow · on “Rx: Should API calls be Singles or Observables of Started, Success, Error events?”
Yes I know. No I just need to notify the ui that made the request, about the state of the request (show progressbars, show actual data, show errors). Kind of like Jake Wharton here [this video] The problem is that I would like to attach data to all these events, not just the success. For example, load more pagination. I need to attach page index so ui can decide to show big fullscreen progressbar (first page), or progressbar in the last item of adapter (2nd+ page)
Cites Dariusz Seweryn · 2017-06 · Stack Overflow · on “Using multiple .subscribe() statements with RxJava”
What is wrong with storing RxBleConnection in a variable and using it for multiple .subscribe() s? (...) What does it mean to say that multiple subscriptions "introduce state", and how can this cause problems? These two questions are almost the same. An RxBleConnection is an abstraction over a state in which a BLE client exchanges some handshake packets with a BLE server after which both the client and the server are considered connected. Unfortunately due to various reasons this connection can be broken at almost any time (and that happens quite often) which a single stored variable of RxBleConnection cannot easily express in a reactive manner. On the other hand observing RxBleDevice.establ
Cites Dariusz Seweryn · 2017-12 · Stack Overflow · on “Resetup notifications in RxAndroidBle if a connection is lost”
Apparently there is a problem with ConnectionSharingAdapter that does not properly release RxBleConnection after an exception was emitted from RxBleDevice.establishConnection() . Using the plain RxJava API should do the trick: subscription = rxBleDevice.establishConnection() .flatMap(rxBleConnection -> rxBleConnection.setupNotification(characteristicUuid)) .doOnNext(notificationObservable -> runOnUiThread(this::notificationHasBeenSetUp)) .flatMap(notificationObservable -> notificationObservable) .retryWhen(errors -> errors.flatMap(error -> { if (error instanceof BleDisconnectedException) { Log.d("Retry", "Retrying"); return Observable.just(null); } return Observable.error(error); })) .observ
Cites tenprint · 2018-04 · Stack Overflow · on “How to emit final value when combining two Observable streams?”
Using Jake Wharton's Managing State with RxJava [this video] pattern. I am combining two api calls together to be executed in parallel. How do I emit a "success" item when both have completed? Please see comments in code below. Thanks! Main calling function: Observable .just(UpdatePicEvent(userId, file)) .compose(updatePic()) <-- Handles updating pic, emits models consumed by UI .mergeWith( Observable .just(UpdateProfileEvent(..params...)) .compose(updateProfile()) <-- Handles updating other settings, emits models consumed by UI ) // TODO Need to add something to emit a Success() model item when both actions above have completed .subscribe(...pass models to UI...) updatePic() fun updatePic(): Obs
Cites tenprint · 2018-04 · Stack Overflow · on “Nothing executes after startWith operator in RxJava”
Trying to follow Jake Wharton's example in Managing State with RxJava [this video] My code works fine without the startWith operator, but as soon as I add it, only the startWith item is emitted, and the rest of the chain does not execute. Here's the calling function: compositeDisposable.add( Observable .just(myUpdateProfileEvent) .compose(update()) .subscribe({ state.value = it }, { Timber.e(it) }) ) And my transformer: fun update(): ObservableTransformer<UpdateProfileEvent, ProfileModel> { return ObservableTransformer { it.flatMap { api .updateUser( … user params …) .subscribeOn(Schedulers.io()) .doOnNext { saveUser(it) } .map { ProfileModel.UpdateSuccessful(it) as ProfileModel } .onErrorReturn