Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Chain Observables #17

Open
MBontekoning opened this issue Jul 28, 2017 · 0 comments
Open

Chain Observables #17

MBontekoning opened this issue Jul 28, 2017 · 0 comments

Comments

@MBontekoning
Copy link

Hi,

I am new to the Reactive framework and I have a question about your library. I want to save a ParseObject(1) that has several ParseObject(2)s attached to it through a relation. Each of these ParseObject(2)'s has a ParseFile attached. So I want to make sure that I create all of these objects in the right order. My approach was:

final ParseObject1 parseObject1 = new ParseObject1();
final ParseRelation<ParseObject> imagesRelation = parseObject1.getRelation("images");

Observable.just(1)
    .subscribeOn(Schedulers.io())
    .flatMap(new Function<Object, ObservableSource<ParseFile>>() {
        @Override
        public ObservableSource<ParseFile> apply(Object aVoid) throws Exception {
            bytes[] bytes = (..);
            ParseFile file = new ParseFile("1.jpg", bytes);

            return ParseObservable.save(file);
        }
    }).flatMap(new Function<ParseFile, ObservableSource<ParseObject2>>() {
        @Override
        public ObservableSource<ParseObject2> apply(ParseFile file) throws Exception {
            ParseObject2 parseObject2 = new ParseObject2();
            parseObject2.put("file", file);
            return ParseObservable.save(parseObject2);
        }
    })
    .flatMap(new Function<ParseObject2, ObservableSource<ParseFile>>() {
        @Override
        public ObservableSource<ParseFile> apply(ParseObject2 parseObject2) throws Exception {
            imagesRelation.add(parseObject2);

            bytes[] bytes = (..);
            ParseFile file = new ParseFile("2.jpg", bytes);

            return ParseObservable.save(file);
        }
    })
    .flatMap(new Function<ParseFile, ObservableSource<ParseObject2>>() {
        @Override
        public ObservableSource<ParseObject2> apply(ParseFile parseFile) throws Exception {
            ParseObject2 parseObject2 = new ParseObject2();
            parseObject2.put("file", parseFile);

            return ParseObservable.save(parseObject2);
        }
    })
    .flatMap(new Function<ParseObject2, ObservableSource<ParseObject1>>() {
        @Override
        public ObservableSource<ParseObject1> apply(ParseObject2 parseObject2) throws Exception {
            imagesRelation.add(parseObject2);

            parseObject1.put("description", "This is an object");
            return ParseObservable.save(parseObject1);
        }
    })
    .subscribe(new Observer<Object>() {
        @Override
        public void onSubscribe(Disposable d) {
        }

        @Override
        public void onNext(Object value) {
        }

        @Override
        public void onError(Throwable e) {
        }

        @Override
        public void onComplete() {
            Toast.makeText(getContext(), "added!", Toast.LENGTH_SHORT).show();
        }
    });

But only the first .flatMap is executed. I want the whole chain to be executed. Any idea how I can transform my code?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants