2017-01-02 7 views
0

블로그에서 게시물로드를 시도하고 있습니다. 나는 mosby + retrofit + 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가 작동하지 않습니다. 난 항상 내가

tmp = api.getPostList(1000) 

그때 내가 진정한 쿼리 문자열을 얻을 작성하는 경우 콘솔 쓰기

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

에서 같은 가져 오기 = 0 last_id와 쿼리 만 라인을 얻을 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 변수를 새 스트림으로 바꾸는 것입니다.

그러나 이것은 최적의 방법이 아닙니다. 기존 솔루션은 구독자가 다른 스트림에 가입했을 때 이전 구독자에게 영향을 미치지 않으므로 주제에 대해 살펴보고 이전 스트림에서 새로운 값을 방출해야합니다.