问题描述
执行任务并最终使用 Flowable rxjva3 返回值。我有以下代码
public Maybe<List<String>> uploadobject(Publisher<CompletedFileUpload> images) {
Storage storage = StorageOptions.getDefaultInstance().getService();
var returnValue = Flowable.frompublisher(images)
.collect((List<String> returnImages,CompletedFileUpload image) -> {
BlobId blobId = BlobId.of(googleUploadobjectConfiguration.bucketName(),image.getName());
BlobInfo blobInfo = BlobInfo.newBuilder(blobId).build();
Blob updatedImage = storage.create(blobInfo,image.getBytes());
returnImages.add(updatedImage.getName());
})
.flatMapMaybe(returnImages -> Maybe.just(returnImages));
}
基本上,它会迭代并将图像上传到谷歌存储。那么返回的媒体 URL 应该返回到 String 的列表中。然而,尝试了下面的代码,返回类型是 Maybe<U>
。执行此操作的正确方法是什么?
更新 1
Flowable.frompublisher(images).collect(ArrayList::new,(returnImages,image) -> {
BlobId blobId = BlobId.of(googleUploadobjectConfiguration.bucketName(),image.getName());
BlobInfo blobInfo = BlobInfo.newBuilder(blobId).build();
Blob updatedImage = storage.create(blobInfo,image.getBytes());
returnImages.add(updatedImage.getName());
LOG.info(
String.format("File %s uploaded to bucket %s as %s",image.getName(),googleUploadobjectConfiguration.bucketName(),image.getName())
);
}).flatMapMaybe((returnImages)-> List.of(returnImages));
这也不对,返回类型应该是Maybe<List<String>>
解决方法
从注释中,使用两个参数 collect
,然后使用 toMaybe
。您可能需要加强集合类型,如下所示:
Flowable.fromPublisher(images)
.<List<String>>collect(ArrayList::new,(returnImages,image) -> {
BlobId blobId = BlobId.of(googleUploadObjectConfiguration.bucketName(),image.getName());
BlobInfo blobInfo = BlobInfo.newBuilder(blobId).build();
Blob updatedImage = storage.create(blobInfo,image.getBytes());
returnImages.add(updatedImage.getName());
LOG.info(
String.format("File %s uploaded to bucket %s as %s",image.getName(),googleUploadObjectConfiguration.bucketName(),image.getName())
);
}).toMaybe();