合并到 Observables/Singles 的方法有哪些? [英] What are the ways to merge to Observables / Singles?

查看:38
本文介绍了合并到 Observables/Singles 的方法有哪些?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个代码:

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 对象.如何做呢?

In the code above what I need to do is to be able to access the entity object at the point in the comment. How can it be done?

推荐答案

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

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

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

Create a class to wrap the emission of Entity and the result of 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;
        }
    }

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

Then change your code to emit that class:

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 类,而是从这个线程.如果您使用 Kotlin,则有一个 Pair 班级.

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.

这篇关于合并到 Observables/Singles 的方法有哪些?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆