使用Rx和Retrofit过滤响应



我在Rx中有一个这样的调用:

rxHelper.manageSubscription(HavocService.getInstance().getHavocAPI().getAllTasks(userId)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .compose(RxTiPresenterUtils.deliverLatestToView(this))
                .subscribe(response -> {
                    this.mStandardTaskResponse = response;
                    mListOfTasks = mStandardTaskResponse.getTasks();
                    getView().setTaskList(mListOfTasks);
                    getView().setLoading(false);
                }, throwable -> {
                    getView().setLoading(false);
                    throwable.printStackTrace();
                })
        );

,其中response为以下类型

public class StandardTaskResponse {
    /**
     * Whether or not there was an error with the response
     */
    public boolean status;
    /**
     * List of Tasks
     */
    @SerializedName("doc")
    public List<Task> tasks;
    /**
     * Gets the array of Tasks from the response
     *
     * @return the List of Tasks
     */
    public List<Task> getTasks() {
        return tasks;
    }
}

我想过滤响应的任务列表。例如,我只希望List包含以下项目:例如TaskFromList.getStatus() == 2

我如何用Rx做这个?

这就是我最后做的

rxHelper.manageSubscription(HavocService.getInstance().getHavocAPI().getAllTasks(USER)
                .flatMap(response -> Observable.from(response.getTasks()))
                //filter out Tasks that are ARCHIVED or DONE
                .filter(task -> task.getStatus() == TaskStatusEnum.INCOMPLETE)
                .toList()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .compose(RxTiPresenterUtils.deliverLatestToView(this))
                .subscribe(list -> {
                    mListOfTasks = list;
                    getView().setTaskList(mListOfTasks);
                    getView().setLoading(false);
                }, throwable -> {
                    getView().setLoading(false);
                    throwable.printStackTrace();
                })
        );

相关内容

  • 没有找到相关文章

最新更新