使用 Rx 和 Retrofit 过滤响应 [英] Filter a response using Rx and Retrofit

查看:42
本文介绍了使用 Rx 和 Retrofit 过滤响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Rx 中有一个这样的电话:

I have a call in Rx like so:

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 的项目.

I want to filter the response's Task list. For example, I only want the List to include items where TaskFromList.getStatus() == 2 for example.

我如何用 Rx 做到这一点?

How do I do this with 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();
                })
        );

这篇关于使用 Rx 和 Retrofit 过滤响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆