Perform task and finally return the value using Flowable rxjva3. I have below code
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));
}
Basically, it iterates and uploads the image to google storage. Then the return media URL should return to the list of String. Tried the below code however, the return type is Maybe<U>
. What is the proper way of performing this?
Update 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));
This is also not correct, the return type should be Maybe<List<String>>
From the comments, use the two argument collect
and then use toMaybe
. You may have to reinforce the collection type as shown below:
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();