通过改造rxjava concatWith时,堆栈溢出 [英] Stack overflow when using Retrofit rxjava concatWith

查看:453
本文介绍了通过改造rxjava concatWith时,堆栈溢出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用rxjava观测来处理分页的改造。我跟着从另一个问题的意见。

I want to handle pagination in Retrofit using rxjava Observable. I followed the advice from another question.

我有需要获取100多页,但连锁失败的20页左右,并停止任何进一步认购可观察的下方日志中的logcat

I have more than 100 pages that needs to be fetched, but chain fails around the 20th page and stops any further subscription to the observable with the below log in the logcat

04-04 04:12:11.766    2951-3012/com.example.app I/dalvikvm﹕ threadid=28: stack overflow on call to Ljava/util/concurrent/atomic/AtomicLongFieldUpdater$CASUpdater;.compareAndSet:ZLJJ
04-04 04:12:11.766    2951-3012/com.example.app I/dalvikvm﹕ method requires 56+20+32=108 bytes, fp is 0x94b52350 (80 left)
04-04 04:12:11.766    2951-3012/com.example.app I/dalvikvm﹕ expanding stack end (0x94b52300 to 0x94b52000)
04-04 04:12:11.766    2951-3012/com.example.app I/dalvikvm﹕ Shrank stack (to 0x94b52300, curFrame is 0x94b548dc)

有谁知道为什么会发生?

Does anybody know why this might happen?

更新:我知道这种情况,由于递归,但有处理分页与改造和rxjava一个更优雅的方式

Update: I know this happens due to recursion, but is there a more graceful way of handling pagination with retrofit and rxjava?

推荐答案

所以,给我回答你引用的我也许应该尝试回答这种情况下,以及在原来的问题。 :)

So, given I answered the original question you referenced I should probably try to answer this case as well. :)

这是另一个有效的(和潜在的简单)的替代我原来的页面的答案,现在,我已经开发了几个接收招数在我的阿森纳。 :)(在java8拉姆达式伪code完成):

This is another valid (and potentially simpler) alternative to my original paging answer, now that I've developed a few more Rx tricks in my arsenal. :) (Done in java8 lambda-style pseudocode):

Observable.range(Integer.MAX_VALUE)
    // Get each page in order.
    .concatMap(page -> getResults(page))
    // Take every result up to and including the one where the next page index is null.
    .takeUntil(result -> result.next == null)
    // Add each output to a list builder. I'm using Guava's ImmutableList, but you could
    // just as easily use a regular ArrayList and avoid having to map afterwards. I just
    // personally prefer outputting an immutable data structure, and using the builder
    // is natural for this.
    //
    // Also, if you wanted to have the observable stream the full output at each page,
    // you could use collect instead of reduce. Note it has a slightly different syntax. 
    .reduce(
        ImmutableList.<ResponseObject>builder(),
        (builder, response) -> builder.addAll(response.results))
    // Convert list builder to one List<ResponseObject> of all the things.
    .map(builder -> builder.build())
    .subscribe(results -> { /* Do something with results. */ });

这篇关于通过改造rxjava concatWith时,堆栈溢出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆