RX Java中的所有元素都达到一定条件

时间:2018-10-04 16:16:03

标签: java amazon-s3 rx-java

我正在尝试使用RxJava 2以异步方式对S3执行MultiPart upload

compile "io.reactivex.rxjava2:rxjava:2.2.2"

我有Flowable个零件。对于每个部分,我在TransferManager对象上调用一个方法,该方法立即返回一个Upload对象。

Upload::isDone方法javadoc如下:

/** * Returns whether or not the transfer is finished (i.e. completed successfully, * failed, or was canceled). * * @return Returns <code>true</code> if this transfer is finished (i.e. completed successfully, * failed, or was canceled). Returns <code>false</code> if otherwise. */ public boolean isDone();

这就是我想要做的:

TransferManager

public class TransferManager {

    public Upload transfer(Part part) {
        return new Upload();
    }
}

Part

public class Part {}

Upload

import java.util.Random;

public class Upload {
    public boolean isDone() {
        return new Random().nextInt(10) == 1;
    }
}

这就是我在做什么:

  TransferManager tm = new TransferManager();
        Flowable.just(new Part(), new Part(), new Part(), new Part(), new Part())
                .map(part -> tm.transfer(part))
                .subscribe(upload -> {
                    do {
                    } while(!upload.isDone());
                });

RxJava正确的方法是什么?

1 个答案:

答案 0 :(得分:0)

如果我理解您说的没错,您想知道何时upload.isDone(),然后在订阅中执行一些操作

.map(part -> tm.transfer(part))
.filter(upload -> upload.isDone())
.subscribe(upload -> {...});

我认为,这可以帮助