RxJava - fetch every item on the list and emits one-by-one (any order)
I have a method that returns an Observable<List<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>
. currently I'm doing it by below code.
@Override
public Observable<List<Item>> getResponses(List<Long> requests) {
return Observable.from(requests).
flatMap((Func1<Long, Observable<Item>>) restRequest -> getResponseEach(restRequest)).toList();
}
It's working fine, but it returning all the response in on go, I mean when all download get finish then my onNext()
get invoked,
Main Question But alternatively I need to emit every response one-by-one(Any Order) once each item fetched successfully from server, so my onNext
should be invoked every-time time individually for each item.
How would I do this using RxJava operators?

add a comment |
I have a method that returns an Observable<List<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>
. currently I'm doing it by below code.
@Override
public Observable<List<Item>> getResponses(List<Long> requests) {
return Observable.from(requests).
flatMap((Func1<Long, Observable<Item>>) restRequest -> getResponseEach(restRequest)).toList();
}
It's working fine, but it returning all the response in on go, I mean when all download get finish then my onNext()
get invoked,
Main Question But alternatively I need to emit every response one-by-one(Any Order) once each item fetched successfully from server, so my onNext
should be invoked every-time time individually for each item.
How would I do this using RxJava operators?

add a comment |
I have a method that returns an Observable<List<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>
. currently I'm doing it by below code.
@Override
public Observable<List<Item>> getResponses(List<Long> requests) {
return Observable.from(requests).
flatMap((Func1<Long, Observable<Item>>) restRequest -> getResponseEach(restRequest)).toList();
}
It's working fine, but it returning all the response in on go, I mean when all download get finish then my onNext()
get invoked,
Main Question But alternatively I need to emit every response one-by-one(Any Order) once each item fetched successfully from server, so my onNext
should be invoked every-time time individually for each item.
How would I do this using RxJava operators?

I have a method that returns an Observable<List<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>
. currently I'm doing it by below code.
@Override
public Observable<List<Item>> getResponses(List<Long> requests) {
return Observable.from(requests).
flatMap((Func1<Long, Observable<Item>>) restRequest -> getResponseEach(restRequest)).toList();
}
It's working fine, but it returning all the response in on go, I mean when all download get finish then my onNext()
get invoked,
Main Question But alternatively I need to emit every response one-by-one(Any Order) once each item fetched successfully from server, so my onNext
should be invoked every-time time individually for each item.
How would I do this using RxJava operators?


edited Nov 21 '18 at 5:26
Sarath Kn
1,613919
1,613919
asked Nov 21 '18 at 4:52


Lavekush AgrawalLavekush Agrawal
4,36563773
4,36563773
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
You have to remove the toList()
operator. The toList()
will emit only after all the emissions of the upstream have been completed, and it will collect the results and will emit as a Single<List<YourResultObject>>
You can return the observable returned by the flatMap
in your code and you will get the results one by one,
public Observable<Item> getResponses(List<Long> requests) {
return Observable.fromIterable(requests)
.flatMap(restRequest -> getResponseEach(restRequest));
}
Now, your getResponses
method will return Observable<Item>
instead of Observable<List<Item>>
And you can subscribe to this function as follows
getResponses(ids)
.subscribe(new DisposableObserver<Item>() {
@Override
public void onNext(Item item) {
// each item will be received here one by one
}
@Override
public void onError(Throwable e) {
// handle any occured error during the operation
}
@Override
public void onComplete() {
// all operations completed
}
});
Already tried seems not working for me.
– Lavekush Agrawal
Nov 21 '18 at 5:54
Pls post your full code. Till the subscription
– Sarath Kn
Nov 21 '18 at 5:55
yourgetResponseEach
returnsObservable<Item>
right?
– Sarath Kn
Nov 21 '18 at 5:59
yes, but instead ofObservable.fromIterable
I'm usingObservable.from()
, just bcozfromIterable
in available.
– Lavekush Agrawal
Nov 21 '18 at 6:28
You are using Rxjava 1 or 2
– Sarath Kn
Nov 21 '18 at 6:37
|
show 3 more comments
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53405454%2frxjava-fetch-every-item-on-the-list-and-emits-one-by-one-any-order%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
You have to remove the toList()
operator. The toList()
will emit only after all the emissions of the upstream have been completed, and it will collect the results and will emit as a Single<List<YourResultObject>>
You can return the observable returned by the flatMap
in your code and you will get the results one by one,
public Observable<Item> getResponses(List<Long> requests) {
return Observable.fromIterable(requests)
.flatMap(restRequest -> getResponseEach(restRequest));
}
Now, your getResponses
method will return Observable<Item>
instead of Observable<List<Item>>
And you can subscribe to this function as follows
getResponses(ids)
.subscribe(new DisposableObserver<Item>() {
@Override
public void onNext(Item item) {
// each item will be received here one by one
}
@Override
public void onError(Throwable e) {
// handle any occured error during the operation
}
@Override
public void onComplete() {
// all operations completed
}
});
Already tried seems not working for me.
– Lavekush Agrawal
Nov 21 '18 at 5:54
Pls post your full code. Till the subscription
– Sarath Kn
Nov 21 '18 at 5:55
yourgetResponseEach
returnsObservable<Item>
right?
– Sarath Kn
Nov 21 '18 at 5:59
yes, but instead ofObservable.fromIterable
I'm usingObservable.from()
, just bcozfromIterable
in available.
– Lavekush Agrawal
Nov 21 '18 at 6:28
You are using Rxjava 1 or 2
– Sarath Kn
Nov 21 '18 at 6:37
|
show 3 more comments
You have to remove the toList()
operator. The toList()
will emit only after all the emissions of the upstream have been completed, and it will collect the results and will emit as a Single<List<YourResultObject>>
You can return the observable returned by the flatMap
in your code and you will get the results one by one,
public Observable<Item> getResponses(List<Long> requests) {
return Observable.fromIterable(requests)
.flatMap(restRequest -> getResponseEach(restRequest));
}
Now, your getResponses
method will return Observable<Item>
instead of Observable<List<Item>>
And you can subscribe to this function as follows
getResponses(ids)
.subscribe(new DisposableObserver<Item>() {
@Override
public void onNext(Item item) {
// each item will be received here one by one
}
@Override
public void onError(Throwable e) {
// handle any occured error during the operation
}
@Override
public void onComplete() {
// all operations completed
}
});
Already tried seems not working for me.
– Lavekush Agrawal
Nov 21 '18 at 5:54
Pls post your full code. Till the subscription
– Sarath Kn
Nov 21 '18 at 5:55
yourgetResponseEach
returnsObservable<Item>
right?
– Sarath Kn
Nov 21 '18 at 5:59
yes, but instead ofObservable.fromIterable
I'm usingObservable.from()
, just bcozfromIterable
in available.
– Lavekush Agrawal
Nov 21 '18 at 6:28
You are using Rxjava 1 or 2
– Sarath Kn
Nov 21 '18 at 6:37
|
show 3 more comments
You have to remove the toList()
operator. The toList()
will emit only after all the emissions of the upstream have been completed, and it will collect the results and will emit as a Single<List<YourResultObject>>
You can return the observable returned by the flatMap
in your code and you will get the results one by one,
public Observable<Item> getResponses(List<Long> requests) {
return Observable.fromIterable(requests)
.flatMap(restRequest -> getResponseEach(restRequest));
}
Now, your getResponses
method will return Observable<Item>
instead of Observable<List<Item>>
And you can subscribe to this function as follows
getResponses(ids)
.subscribe(new DisposableObserver<Item>() {
@Override
public void onNext(Item item) {
// each item will be received here one by one
}
@Override
public void onError(Throwable e) {
// handle any occured error during the operation
}
@Override
public void onComplete() {
// all operations completed
}
});
You have to remove the toList()
operator. The toList()
will emit only after all the emissions of the upstream have been completed, and it will collect the results and will emit as a Single<List<YourResultObject>>
You can return the observable returned by the flatMap
in your code and you will get the results one by one,
public Observable<Item> getResponses(List<Long> requests) {
return Observable.fromIterable(requests)
.flatMap(restRequest -> getResponseEach(restRequest));
}
Now, your getResponses
method will return Observable<Item>
instead of Observable<List<Item>>
And you can subscribe to this function as follows
getResponses(ids)
.subscribe(new DisposableObserver<Item>() {
@Override
public void onNext(Item item) {
// each item will be received here one by one
}
@Override
public void onError(Throwable e) {
// handle any occured error during the operation
}
@Override
public void onComplete() {
// all operations completed
}
});
edited Nov 21 '18 at 5:37
answered Nov 21 '18 at 5:26
Sarath KnSarath Kn
1,613919
1,613919
Already tried seems not working for me.
– Lavekush Agrawal
Nov 21 '18 at 5:54
Pls post your full code. Till the subscription
– Sarath Kn
Nov 21 '18 at 5:55
yourgetResponseEach
returnsObservable<Item>
right?
– Sarath Kn
Nov 21 '18 at 5:59
yes, but instead ofObservable.fromIterable
I'm usingObservable.from()
, just bcozfromIterable
in available.
– Lavekush Agrawal
Nov 21 '18 at 6:28
You are using Rxjava 1 or 2
– Sarath Kn
Nov 21 '18 at 6:37
|
show 3 more comments
Already tried seems not working for me.
– Lavekush Agrawal
Nov 21 '18 at 5:54
Pls post your full code. Till the subscription
– Sarath Kn
Nov 21 '18 at 5:55
yourgetResponseEach
returnsObservable<Item>
right?
– Sarath Kn
Nov 21 '18 at 5:59
yes, but instead ofObservable.fromIterable
I'm usingObservable.from()
, just bcozfromIterable
in available.
– Lavekush Agrawal
Nov 21 '18 at 6:28
You are using Rxjava 1 or 2
– Sarath Kn
Nov 21 '18 at 6:37
Already tried seems not working for me.
– Lavekush Agrawal
Nov 21 '18 at 5:54
Already tried seems not working for me.
– Lavekush Agrawal
Nov 21 '18 at 5:54
Pls post your full code. Till the subscription
– Sarath Kn
Nov 21 '18 at 5:55
Pls post your full code. Till the subscription
– Sarath Kn
Nov 21 '18 at 5:55
your
getResponseEach
returns Observable<Item>
right?– Sarath Kn
Nov 21 '18 at 5:59
your
getResponseEach
returns Observable<Item>
right?– Sarath Kn
Nov 21 '18 at 5:59
yes, but instead of
Observable.fromIterable
I'm using Observable.from()
, just bcoz fromIterable
in available.– Lavekush Agrawal
Nov 21 '18 at 6:28
yes, but instead of
Observable.fromIterable
I'm using Observable.from()
, just bcoz fromIterable
in available.– Lavekush Agrawal
Nov 21 '18 at 6:28
You are using Rxjava 1 or 2
– Sarath Kn
Nov 21 '18 at 6:37
You are using Rxjava 1 or 2
– Sarath Kn
Nov 21 '18 at 6:37
|
show 3 more comments
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53405454%2frxjava-fetch-every-item-on-the-list-and-emits-one-by-one-any-order%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown