簡體   English   中英

RxJava2:需要幫助來解決代碼中的問題

[英]RxJava2: need help to fix an issue in code

我是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)
                )
            );
} 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM