如何添加Observable <list> <items>&gt;列出<items> RxJava Android

时间:2017-06-21 17:00:17

标签: android rx-java observable retrofit2

在我的存储库中,我有一个Retrofit调用,返回一个Observable&gt;

 public Observable<List<Task>> getTasks() {

    final List<Task> tasks = new ArrayList<>();

    return TaskService.networkCall().getTasks()
            .flatMap(new Func1<Response<ResponseObject>, Observable<List<Task>>>() {
                @Override
                public Observable<List<Task>> call(Response<ResponseObject> responseObjectResponse) {
                    List<Task> response = responseObjectResponse.body().getTasksResponse();
                    tasks.addAll(response);
                    return Observable.just(tasks); // this have the list of tasks - I checked
                }
            });

当我尝试在我的viewModel中添加并返回List时遇到的问题:

 public List<Task> getTaskList() {

    final List<Task> cache = new ArrayList<>();

    mSubscription.add(mDataManager.getTasks()
    .subscribeOn(Schedulers.io())
    .observeOn(AndroidSchedulers.mainThread())
    .subscribe(new Action1<List<Task>>() {
        @Override
        public void call(List<Task> tasks) { // List<Task> has data

           cache.addAll(tasks); // List<Task> cache is always 0 size

        }
    }, new Action1<Throwable>() {
        @Override
        public void call(Throwable throwable) {

        }
    }));

我做错了什么?

0 个答案:

没有答案