-1
public Single<Content> createContent(final Content content) {
        BackendACL acl = new BackendACL();
        acl.setPublicRead(false);
        acl.setPublicWrite(false);
        content.setAcl(acl);
        return content.create().flatMap(backendEntity -> {
            Content newContent = (Content) backendEntity;
            newContent.setEntityId(backendEntity.getEntityId());
            newContent.setSchema(content.getSchema());
            return Single.just(newContent);
        }).flatMap(content1 -> {
            return content1.setLink("schema", content1.getSchema().getEntityId());
        }).flatMap(isLinked -> {
            // Code section 1
            //return content1.setLink("user", getLoggedInUser().getEntityId());
            return Single.just(true); // just a dummy to  illustrate
        }).flatMap(isLinked -> {
            return Single.just(content);
            // Code section 2
            // should be: return Single.jus(content1);
        });
}

In the code above, what is the solution to use the content1 variable in Code section 1 and Code section 2?

quarks
  • 33,478
  • 73
  • 290
  • 513
  • What does `setLink` do? Its name suggests that it sets the link of the content, but judging from the usage, it also seems to return a value? – Sweeper Sep 02 '19 at 08:40
  • please see this [answer](https://stackoverflow.com/a/57542520/1401164) – Gustavo Pagani Sep 02 '19 at 08:41
  • Possible duplicate of [What are the ways to merge to Observables / Singles?](https://stackoverflow.com/questions/57541365/what-are-the-ways-to-merge-to-observables-singles) – Gustavo Pagani Sep 02 '19 at 08:46
  • @Gustavo I don't think this is a duplicated because the method `setLink` is a `Single` not just a boolean value. – quarks Sep 02 '19 at 13:09
  • Meaning you can do `content1.setLink("schema", content1.getSchema().getEntityId()).subscribe(...)` thus the question is how to call setLink but also return the `content1` – quarks Sep 02 '19 at 13:11

1 Answers1

1

Between one operator and another you can only emit one object type. In your situation you are emitting a boolean value to code section 1, but you also want to have access to the Content instance. The solution is to wrap both values (Content object and boolean value) in a single class and emit that class.

Create a class to wrap the emission of Content and the result of setLink.

    class Pair {
        private final Content content;
        private final boolean isLinked;

        private Pair(Content content, boolean isLinked) {
            this.content = content;
            this. isLinked = isLinked;
        }

        public Content getContent() {
            return content;
        }

        public boolean isLinked() {
            return isLinked;
        }
    }

Then change your code to emit that class:

      return content.create().flatMap(backendEntity -> {
            Content newContent = (Content) backendEntity;
            newContent.setEntityId(backendEntity.getEntityId());
            newContent.setSchema(content.getSchema());
            return Single.just(newContent);
        }).flatMap(content1 -> {
            return content1.setLink("schema", content1.getSchema().getEntityId())
                .flatMap(isLinked -> Single.just(new Pair(content1, isLinked)));
        }).flatMap(pair -> {
            // Code section 1
            // ** here you can access pair.getContent() and pair.isLinked()
            return Single.just(true); // just a dummy to  illustrate
        })

Ps: instead of creating your own Pair class, check one of the options from this thread. If you are using Kotlin, there is a Pair class.

Gustavo Pagani
  • 6,583
  • 5
  • 40
  • 71