Paginate Observable results without recursion - RxJava

后端 未结 4 547
天命终不由人
天命终不由人 2020-12-01 20:09

I\'ve got a pretty standard API pagination problem which you can handle with some simple recursion. Here\'s a fabricated example:

public Observable

        
4条回答
  •  醉话见心
    2020-12-01 20:34

    Just an idea, why wouldnt you implement your own iterable that iterates over your pages and then make an observer from it?

    Example :

    Observable.from(new Iterable() {
    
            @Override
            public Iterator iterator() {
                return new Iterator() {
                    @Override
                    public boolean hasNext() {
                        return hasNextPage(currentPageKey);
                    }
    
                    @Override
                    public T next() {
    
                        page = getNextPage(currentPageKey);
                        currentPageKey = page.getKey();
                        return page;
                    }
    
                    @Override
                    public void remove() {
                        throw new UnsupportedOperationException();
                    }
                };
            }
        });
    

    a more elegant way would be to make your page manager (the scanner variable in your code example i believe) implement iterable and write the iteration logic there.

提交回复
热议问题