2017-01-02 66 views
0

我正在嘗試從博客加載帖子。我使用mosby +翻新+ rxjava。加載更多關於翻新和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不起作用。 我總是在控制檯寫

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

與last_id = 0相同的get查詢,但行,如果我寫

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

你的問題就出在這個代碼片段:對觀測

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

運營商進行不變操作,這意味着它總是返回新流這是前一個的修改版本。這意味着,當您應用mergeWith運營商時,由於您未將其存儲在任何位置,因此導致此結果丟失。最容易解決的是用新的流替換舊的postList變量。

但是,這並不是最佳的做法。您應該看看主題,並在舊版流中發佈新值,因爲您當前的解決方案不會影響以前的訂閱者,因爲他們訂閱了不同的流