【问题标题】:What are the ways to merge to Observables / Singles?合并两个 Observables / Singles 的方法是什么?
【发布时间】:2019-08-18 03:11:31
【问题描述】:

我有这个代码:

public void testGetBlob() throws RequestException {
    TestData.getNewApplication().flatMap(testApplication -> {
        Client.initialize(testApplication.getAppId(), testApplication.getApiToken(), testApplication.getMasterKey());
        assertNotNull(testApplication.getApiToken());
        assertNotNull(testApplication.getAppId());
        assertNotNull(testApplication.getMasterKey());
        Entity entity = new Entity("Todo");
        return entity.create();
    }).flatMap(entity -> entity.setBlobProperty("text", "Hello world!".getBytes("UTF-8")))
            .flatMap(isSuccess -> {
                if(isSuccess) {
                    // need to access `entity` at this point
                    return Single.just(isSuccess);
                } else {
                    return Single.just(false);
                }
            }).subscribe(success -> {
        Browser.getWindow().getConsole().log("Blob created");
        finishTest();
    }, error -> {
        Browser.getWindow().getConsole().error(error.getMessage());
        fail();
    });
    delayTestFinish(5000);
}

在上面的代码中,我需要做的是能够访问注释中的entity 对象。怎么做到呢?

【问题讨论】:

    标签: java rx-java


    【解决方案1】:

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

    创建一个类来包装Entity的发射和setBlobProperty的结果。

        class Pair {
            private final Entity entity;
            private final boolean success;
    
            private Pair(Entity entity, boolean success) {
                this.entity = entity;
                this.success = success;
            }
    
            public Entity getEntity() {
                return entity;
            }
    
            public boolean isSuccess() {
                return success;
            }
        }
    

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

    public void testGetBlob() throws RequestException {
        TestData.getNewApplication().flatMap(testApplication -> {
    // ...
            return entity.create();
        }).flatMap(entity -> 
            entity.setBlobProperty("text", "Hello world!".getBytes("UTF-8"))
                // 1. Flat map the setBlobProperty call and emit a Pair with the entity and result
                .flatMap(isSuccess -> Single.just(new Pair(entity, isSuccess)))
        )
                .flatMap(pair -> {
                    if(pair.isSuccess()) {
                        // 2. entity is available here via pair.getEntity()
                        return Single.just(isSuccess);
                    } else {
                        return Single.just(false);
                    }
                }).subscribe(success -> {
    // ...
        }
    }
    

    Ps:不要创建自己的 Pair 类,而是检查此 thread 中的选项之一。如果您使用的是 Kotlin,则有一个 Pair 类。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-06-07
      • 1970-01-01
      • 2018-06-23
      • 2010-11-02
      相关资源
      最近更新 更多