【问题标题】:What is the proper way to pass down variable down the chain with RxJava?使用 RxJava 向下传递变量的正确方法是什么?
【发布时间】:2019-09-02 08:27:57
【问题描述】:
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);
        });
}

在上面的代码中,Code section 1Code section 2中使用content1变量的解决方案是什么?

【问题讨论】:

  • setLink 是做什么的?顾名思义就是设置内容的链接,但是从用法来看,好像也是返回值的?
  • 请看answer
  • @Gustavo 我不认为这是重复的,因为 setLink 方法是 Single 而不仅仅是布尔值。
  • 意思是你可以做content1.setLink("schema", content1.getSchema().getEntityId()).subscribe(...) 因此问题是如何调用 setLink 并且返回content1

标签: java rx-java reactivex


【解决方案1】:

在一个运算符和另一个运算符之间,您只能发出一种对象类型。 在您的情况下,您正在向代码部分 1 发出一个布尔值,但您还希望能够访问 Content 实例。 解决方案是将两个值(内容对象和布尔值)包装在一个类中并发出该类。

创建一个类来包装Content 的发射和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;
        }
    }

然后更改您的代码以发出该类:

      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:不要创建自己的 Pair 类,而是检查此 thread 中的选项之一。如果您使用的是 Kotlin,则有一个 Pair 类。

【讨论】:

  • 只是把它传下来只是故事的一半,content1.setLinkSingle&lt;Boolean&gt;
猜你喜欢
  • 2012-04-19
  • 2018-09-23
  • 2019-01-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-11-16
  • 2023-03-23
相关资源
最近更新 更多