I have a method that returns an Observable<ArrayList<Long>>
, which are ids of some Items. I'd like to go through this list and download every Item using another method that returns Observable<Item>
.
How would I do this using RxJava operators?
onNext(): This method is called when a new item is emitted from the Observable. onError(): This method is called when an error occurs and the emission of data is not successfully completed. onComplete(): This method is called when the Observable has successfully completed emitting all items.
RxJava, once the hottest framework in Android development, is dying. It's dying quietly, without drawing much attention to itself. RxJava's former fans and advocates moved on to new shiny things, so there is no one left to say a proper eulogy over this, once very popular, framework.
RxJava is a Java library that enables Functional Reactive Programming in Android development. It raises the level of abstraction around threading in order to simplify the implementation of complex concurrent behavior.
Single and Completable are new types introduced exclusively at RxJava that represent reduced types of Observable , that have more concise API. Single represent Observable that emit single value or error. Completable represent Observable that emits no value, but only terminal events, either onError or onCompleted.
Here's a small self contained example
public class Example { public static class Item { int id; } public static void main(String[] args) { getIds() .flatMapIterable(ids -> ids) // Converts your list of ids into an Observable which emits every item in the list .flatMap(Example::getItemObservable) // Calls the method which returns a new Observable<Item> .subscribe(item -> System.out.println("item: " + item.id)); } // Simple representation of getting your ids. // Replace the content of this method with yours private static Observable<List<Integer>> getIds() { return Observable.just(Arrays.<Integer>asList(1, 2, 3)); } // Replace the content of this method with yours private static Observable<Item> getItemObservable(Integer id) { Item item = new Item(); item.id = id; return Observable.just(item); } }
Please note that Observable.just(Arrays.<Integer>asList(1, 2, 3))
is a simple representation of Observable<ArrayList<Long>>
from your question. You can replace it with your own Observable in your code.
This should give you the basis of what you need.
p/s : Use flatMapIterable
method for this case since it belongs to Iterable
as explained below:
/** * Implementing this interface allows an object to be the target of * the "for-each loop" statement. See * <strong> * <a href="{@docRoot}openjdk-redirect.html?v=8&path=/technotes/guides /language/foreach.html">For-each Loop</a> * </strong> * * @param <T> the type of elements returned by the iterator * * @since 1.5 * @jls 14.14.2 The enhanced for statement */ public interface Iterable<T>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With