android 一个界面多个请求

最近接手外包项目,看代码基本是直接嵌套框架了,两个项目结构写法都差不多,里面还是有能借鉴的地方,今天记录一个。


LinkedList的remove(int index)方法

按照我之前理解,应该是删除对应位置元素,实际不止,先看下注释吧

/**
* Removes the element at the specified position in this list. Shifts any
* subsequent elements to the left (subtracts one from their indices).
* Returns the element that was removed from the list.
*
* @param index the index of the element to be removed
* @return the element previously at the specified position
* @throws IndexOutOfBoundsException {@inheritDoc}
*/

意思大概是这样;在该列表中的指定位置移除元素。转移任何
左边的后续元素(从索引中减去一个元素)。
返回从列表中移除的元素.

也就是说不光会移除,还会返回移除的元素

下面说一下项目中怎么用的

public class TaskPool {

    private List<INetModel> taskQueue = new LinkedList<>();

    public void addTask(INetModel updateModel) {
        taskQueue.add(updateModel);
    }

    public void execute(final OnNetRequestListener onUpDateListener) {
        if (!taskQueue.isEmpty()) {
            INetModel updateModel = taskQueue.remove(0);
            updateModel.request();
        } else
            onUpDateListener.onSuccess();
    }
}

当一个界面有多个请求,但是要等所有请求都完事才返回成功的时候,可以用到。

看代码;

taskPool.addTask(请求1);
taskPool.addTask(请求2);
taskPool.addTask(请求3);
taskPool.execute(this);

执行execute时,先执行请求1,然后移除请求1,如果列表还有数据,再执行请求2,再移除请求2,当都执行完后返回成功。


总结一下,由于没看到remove的含义,所以研究了一下,这种场景应该挺常见的,当然应该有更好的写法,现在只是根据项目来自己总结一下。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值