1 回答

TA貢獻1936條經驗 獲得超7個贊
在一個運算符和另一個運算符之間,您只能發出一種對象類型。在您的情況下,您正在發出一個布爾值,但您還希望能夠訪問 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 類,而是檢查此線程中的選項之一。如果您使用的是 Kotlin,則有一個Pair類。
添加回答
舉報