2

I am trying to get the data from local db by each content asyncrounous,but the issue is that I want to get the data by the order that I retrieve,at first the data for the first conent,than the second etc.,currently I am getting the data in wrong order every time I run the code ,how can I achieve this?

 for (Content content : contents) {
                scoreCardDisposable = AppManagers.getContentManager()
                        .getScoreCardsAndUpdate(content.getId())
                        .observeOn(AndroidSchedulers.mainThread())
                        .subscribe(scoreCards -> {
                           ...
                        });
                compositeDisposable.add(scoreCardDisposable);

           }
2
  • You'll need to use from iterable which is the equivalent of a for loop in rx java, once you get that you can use flatmap for the rest of it or you can probably skip the flatmap also. Commented May 23, 2018 at 12:57
  • @JudeFernandes can you please give some example? Commented May 23, 2018 at 13:08

2 Answers 2

4

To make an observable stream from a List<> you can use Observable.fromIterable operator:

compositeDisposable.add(Observable.fromIterable(contents)
            .flatMap(content -> {
                return AppManagers.getContentManager()
                        .getScoreCardsAndUpdate(content.getId());
            })
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(scoreCards -> {
                ...
            }));

UPD:

You can use a zip operator to combine content with the getScoreCardsAndUpdate result in custom object:

class Result {
    private String content;
    private String result;

    public Result(String content, String result) {
        this.content = content;
        this.result = result;
    }

    public String getContent() {
        return content;
    }

    public String getResult() {
        return result;
    }
}

public Observable<String> getScoreCardsAndUpdate(String content) {
    return Observable.just("result = " + content);
}

@Test
public void test() {
    List<String> contents = Arrays.asList("1", "2", "3", "4");

    Observable.fromIterable(contents)
            .flatMap(content -> {
                return Observable.zip(Observable.just(content), 
                        getScoreCardsAndUpdate(content), Result::new);
            })
            .subscribe(scoreCards -> {
                System.out.println("content = " + scoreCards.getContent() + 
                        ", " + scoreCards.getResult());
            });
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thx,can you please tell me how can I get content reference in subscribe ?
2

You will have to process all the items in a single thread. You can achieve this by adding a scheduler in rxjava2

.subscribeOn(Schedulers.single())

Your code should be

for (Content content : contents) {
            scoreCardDisposable = AppManagers.getContentManager()
                    .getScoreCardsAndUpdate(content.getId())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribeOn(Schedulers.single())
                    .subscribe(scoreCards -> {
                       ...
                    });
            compositeDisposable.add(scoreCardDisposable);

       }

2 Comments

in getScoreCardsAndUpdate I already use subscribeOn(getIOScheduler()),should I change it?
yeah you will have to change it to any serial schedular so that your task gets executed one by one

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.