今天要介绍的是github上一个Android开源框架: android-starter-kit (Rx部分)
链接: https://github.com/qijitech/android-starter-kit
先看变量:
private final BehaviorSubject<View> views = BehaviorSubject.create();
private final HashMap<Integer, Func0<Subscription>> restartables = new HashMap<>(); //请求
private final HashMap<Integer, Subscription> restartableSubscriptions = new HashMap<>(); 请求subscribe后的subscription
private final ArrayList<Integer> requested = new ArrayList<>(); 已经开始的请求对应的id
看第一个方法restartable
public void restartable(int restartableId, Func0<Subscription> factory) {
restartables.put(restartableId, factory);
if (requested.contains(restartableId))
start(restartableId);
}
参数有Id和一个返回Subscription的Func0接口.
先put进去restartables这个HashMap中, 然后检查requested这个ArrayList中有没有传进来的restartableId, 如果有, 就会取消这个请求并重新开始.
stop方法
源码比较简单, 我就不贴了
从requested中去除传入的restartableId, 然后在restartableSubscriptions找到对应的subscription, 并调用unsubscribe.
start方法
首先调用stop方法, 然后把restartableId放进requested中, 然后取出对应的Func0接口并调用, 得到的subscription放进restartableSubscriptions中.
一般并不会直接用restartable方法, 因为这里提供了3个经过封装的方法:
1: restartableFirst方法
public <T> void restartableFirst(int restartableId, final Func0<Observa