java - 使用 RxAndroid 生成树排序结构

标签 java android reactive-programming rx-java rx-android

我正在尝试我的手和 RxAndroid。我的同步适配器查询服务 A 以获取列表页面(大小 n)。对于页面中的每个项目,我必须发出一个生成另一个行(大小为 m)的请求。也就是说,Pages 中的每个项目都有 m 行。我想将所有 n 行组合成 1 个大小为 m*n 的列表,并保存到数据库中。

Observable.create(new Observable.OnSubscribe<Page>(){
        @Override
        public void call(Subscriber<? super Page> subscriber) {

            ArrayList<Page> pages = Utility.getPagesFromServer();
            for (Page page : pages) {
                subscriber.onNext(page);
            }
            subscriber.onCompleted();
        }


    }).map(new Func1<Page, JSONResponse>() {
        @Override
        public JSONResponse call(Page page) {

            return Utility.getJSONObjectContainingLines(page);

        }
    }).map(new Func1<JSONResponse, ArrayList<Line>>() {
        @Override
        public ArrayList<Line> call(JSONResponse jsonResponse) {
            return getLines(jsonResponse.getJSONObject());
        }
    })

我从服务器获取页面,然后从服务器映射并获取每个页面的行,然后解析 JSON 并获取行的 arrayList。我不确定如何从这里继续。到目前为止,我想在每一行上迭代,而不是在每个 ArrayList 上迭代。

最佳答案

最后map( )使用.flatMapIterable( )你将会改变Observable<ArrayList<Line>>Observable<Line>并在 onNext(Line l)迭代它(或使用 forEach( ) ):

    Observable.defer(new Func0<Observable<Page>>() {
        @Override
        public Observable<Page> call() {
            return Observable.from(Utility.getPagesFromServer());
        }
    })
    .map(new Func1<Page, JSONResponse>() {
        @Override
        public JSONResponse call(Page page) {
            return Utility.getJSONObjectContainingLines(page);
        }
    })
    .map(new Func1<JSONResponse, ArrayList<Line>>() {
        @Override
        public ArrayList<Line> call(JSONResponse jsonResponse) {
            return getLines(jsonResponse.getJSONObject());
        }
    })
    .flatMapIterable(new Func1<ArrayList<Line>, Iterable<Line>>() {
        @Override
        public Iterable<Line> call(ArrayList<Line> lines) {
            return lines;
        }
    })
    .subscribeOn(Schedulers.io())
    .observeOn(AndroidSchedulers.mainThread())
    .subscribe(new Action1<Line>() {
        @Override
        public void call(Line line) {
            //Do something with your line
        }
    }, new Action1<Throwable>() {
        @Override
        public void call(Throwable throwable) {
            throwable.printStackTrace();
        }
    }, new Action0() {
        @Override
        public void call() {
            //on complete
        }
    });

或者使用Lambdas :

    Observable.defer(() -> Observable.from(Utility.getPagesFromServer()))
    .map(page -> Utility.getJSONObjectContainingLines(page))
    .map(jsonResponse -> getLines(jsonResponse.getJSONObject()))
    .flatMapIterable(lines -> lines)
    .subscribeOn(Schedulers.io())
    .observeOn(AndroidSchedulers.mainThread())
    .subscribe(line -> {
        //Do something with your line
    }, throwable -> throwable.printStackTrace(), () -> {
        //on complete
    });

更新:
我决定添加一些链接:
Transforming-Observables
flatmap

如您所见,要转换 Observable<ArrayList<Line>>Observable<Line>您应该使用运算符 flatMapIterable()

关于java - 使用 RxAndroid 生成树排序结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31605182/

相关文章:

android - 我怎样才能让屏幕空闲监听器?

kotlin - 如何在 Kotlin 中将 actor 定义为一个类

javascript - 如何使用 react 事件流同时处理鼠标和触摸事件

java - 如何将阻塞代码变成响应式代码?

java - 为数字和单词设置unicode的正则表达式

Java:如果数据库连接卡住或断开,如何重新启动它?

java - 无法从 Firebase 读取数据 - Firebase 数据库错误 : Permission denied

android - 如何在 BroadcastReceiver 类中使用 getApplicationContext?

java - "The method addActionListener(ActionListener) in the type AbstractButton is not applicable for the arguments"

java - 处理初始化期间 JVM 抛出的异常