.withLatestFrom operator

The .withLatestFrom operator in RxJava is similar to .combineLatest, but there’s an important difference in how it works. While .combineLatest emits a new value every time any of the source observables emits a new item, .withLatestFrom only emits when the main observable emits a new item, while using the latest values from the other observable(s).

1. What is .withLatestFrom?

The .withLatestFrom operator combines the most recent value from one or more other observables with the value emitted by the main observable, but it only emits a new item when the main observable emits.

  • Main Observable: The primary observable that dictates when a new emission happens.

  • Latest Observable: The other observable(s) whose most recent values are used when the main observable emits.

Key Points:

  • Emission Trigger: Only the main observable triggers the emission.

  • Latest Values: The latest values from the other observable(s) are combined with the current emission from the main observable.

2. Syntax of .withLatestFrom in Kotlin (RxJava 2/3)

Here’s the basic syntax of .withLatestFrom:

mainObservable.withLatestFrom(
    otherObservable,
    BiFunction { mainItem, otherItem ->
        // Combine mainItem and otherItem here
    }
)
.subscribe { combinedResult ->
    // Handle the combined result
}

3. Difference Between .withLatestFrom and .combineLatest

  • .combineLatest: Emits whenever any observable emits a new value, combining the latest values from all observables.

  • .withLatestFrom: Emits only when the main observable emits a new value, but it uses the latest value from the other observable(s) to produce the emission.

4. When to Use .withLatestFrom?

You use .withLatestFrom when:

  • You want the emission to be driven by one observable (the main observable), but you still want to incorporate the latest values from other observables.

  • You need to combine real-time data from a secondary source with an event-driven main observable.

5. Practical Example: Combining User Actions with Data

Let’s say we have an example where a user clicks a button, and every time they click, we want to display the latest temperature reading from a sensor.

A. Observable Setup:

  • Button Clicks: This will be the main observable that triggers the emission.

  • Temperature Sensor: This will be the other observable whose latest value we want to combine with the button clicks.

Explanation:

  • The buttonClickObservable simulates button clicks that happen every second.

  • The temperatureObservable emits a series of temperature readings.

  • .withLatestFrom combines each button click (main observable) with the latest temperature reading, but only when the button click occurs.

Output (Simulated):

  • In this example, the emissions are triggered by the button clicks, but the latest temperature readings are fetched and combined with each click event.

  • Even though the temperature readings might update at a different frequency, the click events control the timing of the emissions.

6. Real-World Example: Combining User Input with Network Data

Let’s say you have a form where the user enters a keyword and clicks a "Search" button. When the button is clicked, you want to combine the latest keyword from the user with data from a network call (e.g., fetching the latest results).

A. UI Setup (XML):

B. Code Implementation:

Explanation:

  • Button Clicks: The button clicks are the main observable, which triggers the emission.

  • Keyword Input: The keyword input from the user is the other observable, whose latest value is combined with each button click.

  • withLatestFrom: When the search button is clicked, the latest keyword is retrieved and displayed in the TextView.

7. Use Cases for .withLatestFrom in Android

Here are some real-world scenarios where .withLatestFrom is useful:

  • Form Submission: You want to combine the latest form inputs with the click of a "Submit" button, so that only when the button is clicked, the form data is collected and processed.

  • User Actions and Data Synchronization: For example, a user clicks a button to sync data, and you want to combine the latest device status with the button click to perform the sync operation.

  • Combining Inputs: In a game, you might want to combine user actions (like moving a character) with the latest game state before executing a certain action.

8. Handling More than Two Observables

If you need to combine the main observable with more than one latest observable, you can use Function3, Function4, etc., to handle multiple observables.

Here’s how you can combine three observables using .withLatestFrom:

9. Conclusion

The .withLatestFrom operator is a powerful tool when you want the emissions to be driven by one observable (the main observable) while combining it with the latest values from other observables. It's useful for scenarios where you need to act upon an event (like a button click) but want to use the most recent data from another source.

To recap:

  • Purpose: Combine the latest values from one or more observables with the current emission of a main observable.

  • Use cases: Form submissions, combining real-time data with user-triggered actions, and synchronizing events with the latest available data.

  • Difference from .combineLatest: Emission is triggered only by the main observable, not by all observables.

Let me know if you need further clarification or more examples!

Last updated