Reputation: 29877
In my app, I retrieve a list of items 10 at a time until no more items are available. I need to process each item sequentially. If I understand correctly, this can be done using either Observable.just or Single in RxJava. It isn't clear to me though which of these to choose or whether it even makes a difference.
Or am I totally wrong and neither of these are suitable?
Upvotes: 0
Views: 122
Reputation: 1844
If you want to perform an operation on each item of the list than an Observable
is what you will likely want. A Single
, as its name suggests, is for a single emission.
Consider the following:
List<String> items = new ArrayList<>(10);
// lets say this array list is already populated
Observable.fromIterable(items)
// the map operator handles each emission individually
.map(String::length)
.subscribe(System.out::println);
If we used a Single
instead it would look like:
Single.just(items)
.toObservable()
.flatMapIterable(strings -> strings)
.map(String::length)
.subscribe(System.out::println);
Here you can see we have the single emission of items
but in order to process on each string in the list, we need to both convert it to an observable and flat map on that list, so we get each element of the list individually. Esentially this is just a grosser version of using the Observable
directly
Now if we expand this to retrieving batches of data (in groups of 10) the same principles would apply. It depends on how you are retrieving these batches in order to say exactly how it would hook into creation of an Observable
though.
Upvotes: 1