在改造和rxJava上加载更多

时间:2017-01-02 20:51:37

标签: android rx-java retrofit2 mosby

我正在尝试从博客加载帖子。我使用mosby + retrofit + rxjava。

public class PostRepository implements IPostRepository {

    private Api api;

    private long last_id = 0;
    private Single<List<Post>> postList;

    public PostRepository(Api api) {
        this.api = api;
    }

    @Override
    public Single<List<Post>> getList() {
        this.load();
        return postList;
    }

    private void load() {
        Single<List<Post>> tmp;
        Log.d(Configuration.DEBUG_TAG, "Loading " + last_id);
        tmp = api.getPostList(last_id)
            .map(posts -> {
                ArrayList<Post> postList = new ArrayList<>();
                for (PostResponse post : posts) {
                    if (last_id == 0 || last_id > post.id) {
                        last_id = post.id;
                    }
                    postList.add(new Post(
                            post.id,
                            post.thumb,
                            post.created_at,
                            post.title
                    ));
                }
                return postList;
            });
        if (postList == null) {
            postList = tmp;
        } else {
            postList.mergeWith(tmp);
        }
    }

    @Override
    public Single<Post> getDetail(long id) {
        return api.getPost(id)
                .map(postResponse -> new Post(
                        postResponse.id,
                        postResponse.thumb,
                        postResponse.created_at,
                        postResponse.title,
                        postResponse.body
                ));
    }
}

和api

public interface Api {
    @GET("posts")
    Single<PostListResponse> getPostList(@Query("last_id") long last_id);

    @GET("post/{id}")
    Single<PostResponse> getPost(@Path("id") long id);
}

首先查询网站即可。 https://site/posts?last_id=0

但是第二次运行函数getList不起作用。 我总是得到与last_id = 0相同的get查询,但在控制台写行

D/App: Loading 1416
D/App: 1416
D/OkHttp: --> GET https://site/posts?last_id=0 http/1.1

如果我写

tmp = api.getPostList(1000)

然后我得到真正的查询字符串https://site/posts?last_id=1000

更新 我重写了代码库。

public class PostRepository implements IPostRepository {

    private Api api;

    private long last_id = 0;
    private List<Post> postList = new ArrayList<>();
    private Observable<List<Post>> o;

    public PostRepository(Api api) {
        this.api = api;
    }

    @Override
    public Single<List<Post>> getList() {
        return load();
    }

    private Single<List<Post>> load() {
        return api.getPostList(last_id)
            .map(posts -> {
                for (PostResponse post : posts) {
                    if (last_id == 0 || last_id > post.id) {
                        last_id = post.id;
                    }
                    postList.add(new Post(
                            post.id,
                            post.thumb,
                            post.created_at,
                            post.title
                    ));
                }
                return postList;
            });
    }

    @Override
    public Single<Post> getDetail(long id) {
        return api.getPost(id)
                .map(postResponse -> new Post(
                        postResponse.id,
                        postResponse.thumb,
                        postResponse.created_at,
                        postResponse.title,
                        postResponse.body
                ));
    }
}

它的工作

1 个答案:

答案 0 :(得分:2)

您的问题在于此代码片段:

if (postList == null) {
    postList = tmp;
} else {
    postList.mergeWith(tmp); // here
}

观察者的操作员正在执行 不可变 操作,这意味着它始终返回 新流 ,这是一个上一个的修改版本。这意味着,当您应用mergeWith运算符时,由于您没有将其存储在任何位置,因此会将其结果丢弃。最容易解决的是用新流替换旧的postList变量。

然而,这不是这样做的最佳方式。您应该查看主题并在旧流中发布新值,因为您当前的解决方案不会影响以前的订阅者,因为他们订阅了不同的流