0
List<String> listExample = Arrays.asList("one", "two", "three");

Think of i have list of string, and using RxJava (Observable.fromArray)

Observable.fromArray(ids.toArray(new String[ids.size()])).subscribe(new Consumer<String>() {
    @Override
    public void accept(@NonNull String s) throws Exception {

    }
});

Here think each string has a Async callback here once i receive that callback it should move to next string. Is it possible using rxjava?

This is the code which i'am having currently, it doesn't wait for till the callback right!.

for (String id : ids) {
    getMessage(id, new urlGenCallback() {
        @Override
        public void urlAndToken(String url, String token, String idx) {

        }
    });
}

Thank you @Geoffrey Marizy

Answer: Check Below For More Info About ConcatMap Visit Here

2
  • Put you answer as an answer, not an edit of your question. Or improve the question which helped you get there. Commented Jun 7, 2017 at 13:32
  • Since the issue is solved, th stack-overflow way of doing things is to mark the answer which solved it as "accepted" Commented Jun 12, 2017 at 15:52

2 Answers 2

1

You are looking to the concatMap operator. A complete article on this topic: https://fernandocejas.com/2015/01/11/rxjava-observable-tranformation-concatmap-vs-flatmap/

Wrap your request as an Observable, then pass it as a parameter to concatMap :

Observable.fromArray(ids.toArray(new String[ids.size()])).concatMap(/* your request returning an Observable */)
Sign up to request clarification or add additional context in comments.

Comments

0
Observable.fromArray(ids.toArray(new String[ids.size()]))
        .concatMap(s -> {
            return Observable.create(new ObservableOnSubscribe<String>() {
                @Override
                public void subscribe(@NonNull ObservableEmitter<String> e) throws Exception {
                    getMessage(s, new urlGenCallback() {
                        @Override
                        public void urlAndToken(String url, String token, String id) {
                            e.onNext("......");
                            e.onComplete();
                        }
                    });
                }
            });
        }).subscribe(new Consumer<String>() {
    @Override
    public void accept(@NonNull String s) throws Exception {
       //All other code goes here
    }
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.