首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用Retrofit rxjava concatWith时堆栈溢出

使用Retrofit rxjava concatWith时堆栈溢出
EN

Stack Overflow用户
提问于 2015-04-04 08:27:07
回答 1查看 4.8K关注 0票数 16

我希望使用rxjava可观察到的方法来处理Retrofit中的分页。我听从了另一个question的建议。

我有超过100页需要获取,但是链在第20页附近失败,停止对logcat中下面的日志的任何进一步订阅。

代码语言:javascript
复制
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)

有人知道为什么会发生这种事吗?

更新:我知道这是由于递归而发生的,但是是否有一种更优雅的方法来使用和rxjava处理分页?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-04-12 20:19:32

所以,既然我回答了你提到的最初的问题,我可能也应该试着回答这个问题。:)

这是我原来的分页回答的另一个有效(也可能更简单)的替代方案,因为我已经在我的武库中开发了更多的Rx技巧。)(用java8 lambda样式的伪码完成):

代码语言:javascript
复制
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. */ });
票数 30
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29444297

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档