gpt4 book ai didi

java - RxJava2 : need help to fix an issue in code

转载 作者:行者123 更新时间:2023-12-02 02:12:20 24 4
gpt4 key购买 nike

我是 RxJava 编程新手,需要帮助来处理我的代码问题。我有以下代码:

public Single<List<Modifications>> loadModificationsImages() {
return Observable.fromCallable(DataStoreRepository::loadModifications)
.subscribeOn(Schedulers.io())
.flatMapIterable(list -> list)
.doOnNext(item -> {
Observable.fromIterable(item.images)
.forEach(image -> {
ApiRepository.getModificationsImages(item.id, image.id)
.subscribeOn(Schedulers.computation())
.retry(3)
.subscribe(response -> {
InputStream is = response.byteStream();

BitmapFactory.Options options = new BitmapFactory.Options();
options.inPreferredConfig = Bitmap.Config.RGB_565;

Bitmap bitmap = BitmapFactory.decodeStream(is, null, options);

String contentSubType = response.contentType().subtype();
String fileName = "modifications_id_" + item.id + "_image_id_" + image.id + "." + contentSubType;
FileUtil.saveBitmap(bitmap, fileName);

Modifications.Images img = new Modifications.Images(image.id, fileName, image.type);
DataStoreRepository.updateModificationsImage(img, item.id);
});
});
})
.toList();
}

它工作得很好,但我需要收集每个 Modifications.Images放入集合中并将其传递给方法 ( DataStoreRepository.updateListOfModificationsImage(List<Modifications.Images> images, int id) ) 以更新数据库。所以,这一行的问题是:

Modifications.Images img = new Modifications.Images(image.id, fileName, image.type);
DataStoreRepository.updateModificationsImage(img, item.id);

它只是用单个项目覆盖数据库中的记录。我尝试通过应用 Collection 来修改给定的代码,但它对我不起作用。

感谢您的帮助!

最佳答案

在回调内订阅几乎总是错误的。否则,您应该toList图像,然后调用批量更新方法:

public Single<List<Modifications>> loadModificationsImages() {
return Observable.fromCallable(DataStoreRepository::loadModifications)
.subscribeOn(Schedulers.io())
.flatMapIterable(list -> list)
.flatMapSingle(item ->
Observable.fromIterable(item.images)
.flatMap(image ->
ApiRepository.getModificationsImages(item.id, image.id)
.subscribeOn(Schedulers.io())
.retry(3)
.map(response -> {
InputStream is = response.byteStream();

BitmapFactory.Options options = new BitmapFactory.Options();
options.inPreferredConfig = Bitmap.Config.RGB_565;

Bitmap bitmap = BitmapFactory.decodeStream(is, null, options);

String contentSubType = response.contentType().subtype();
String fileName = "modifications_id_" + item.id + "_image_id_"
+ image.id + "." + contentSubType;
FileUtil.saveBitmap(bitmap, fileName);

Modifications.Images img = new Modifications.Images(
image.id, fileName, image.type);
return img;
})
)
.toList()
.doOnSuccess(images ->
DataStoreRepository.updateListOfModificationsImage(images, image.id)
)
);
}

关于java - RxJava2 : need help to fix an issue in code,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49818709/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com