Concat两个可完成的RxJava

时间:2018-04-20 07:34:03

标签: android rx-java rx-java2

我的目标是结束completable1completable2[0]。在doOnComplete()之后我使用mContent来创建问题。我尝试了上面的代码,但它没有用。

Completable  completable1;

final Completable[] completable2 = new Completable[1];

completable1 = AppManagers.getContentManager()
    .completeCopy(mContent)
    .toCompletable()
    .doOnComplete(() -> {
        File file = new 
        File(ImageUtils.getThumbNailPathsForLocalContent(mContent.getId(), 0));
        if (!file.exists()) {
            final ContentVideoCache videoCache = 
                AppManagers.getAppContext().getVideoCache();
            Uri dataURI = videoCache.getDataURI(mContent);
            completable2[0] = AppManagers.getContentManager()
                .createThumbnails(mContent.getId(), dataURI)
                .toCompletable();
        }
    });
    completable = completable1.concatWith(completable2[0]);

更新:如果completeCopy(mContent)返回mContent

的不同对象
 Completable completable = AppManagers.getContentManager()
                                .copyIntoShell((Content) selectedContent, mContent)
                                .flatMapCompletable(new Function<Content, CompletableSource>() {
                                    @io.reactivex.annotations.NonNull
                                    @Override
                                    public CompletableSource apply(@io.reactivex.annotations.NonNull Content content) throws Exception {
                                        return generateThumbnailsForNewContent(content);
                                    }
                                });

如果我subscribecompletable,那么它将在return generateThumbnailsForNewContent(content);完成后完成吗?

 private Completable generateThumbnailsForNewContent(Content mContent) {
        File file = new File(ImageUtils.getThumbNailPathsForLocalContent(mContent.getId(), 0));
        if (!file.exists()) {
            final ContentVideoCache videoCache = AppManagers.getAppContext().getVideoCache();
            Uri dataURI = videoCache.getDataURI(mContent);
            return AppManagers.getContentManager().createThumbnails(mContent.getId(), dataURI).toCompletable();
        }
        return Completable.error(new FileNotFoundException());
    }

1 个答案:

答案 0 :(得分:2)

不确定您在此处尝试完成的操作,但andThendefer是拥有dependent-deferred continuations的运营商:

AppManagers.getContentManager()
.completeCopy(mContent)
.toCompletable()
.andThen(Completable.defer(() -> {
    File file = new File(ImageUtils.getThumbNailPathsForLocalContent(
        mContent.getId(), 0));

    if (!file.exists()) {
        final ContentVideoCache videoCache = 
            AppManagers.getAppContext().getVideoCache();

        Uri dataURI = videoCache.getDataURI(mContent);

        return AppManagers.getContentManager()
            .createThumbnails(mContent.getId(), dataURI)
            .toCompletable();
    }
    return Completable.error(new FileNotFoundException());
}));