本庫(kù)的地址:https://github.com/jdeferred/jdeferred
依賴(lài)方法:
compile 'org.jdeferred:jdeferred-android-aar:${version}'
// or
compile 'org.jdeferred:jdeferred-android-aar:${version}@aar'
這個(gè)庫(kù)是流式思想的實(shí)踐,類(lèi)同于rxjava藕漱,但更加簡(jiǎn)單(當(dāng)然功能也沒(méi)有rxjava強(qiáng)大)
核心:Promise<D,F,P>
常用類(lèi):DeferredObject, DeferredManager
1,核心promise的方法:
then() //萬(wàn)能方法:可以傳入DoneCallback, FailCallback, ProgressCallback, DoneFilter, FailFilter, ProgressFilter, DonePipe, FailPipe, ProgressPipe, 返回promise
done() //完成回調(diào)
fail() //失敗回調(diào)
progress() //進(jìn)度回調(diào)
always() //無(wú)論什么結(jié)果都會(huì)回調(diào)
2,DeferredObject
Deferred deferred = new DeferredObject();
deferred.resolve("成功了") //需要傳給done的結(jié)果
deferred.reject("失敗了")//需要傳給fail的結(jié)果
deferred.notify(100)//需要傳給progress的結(jié)果
實(shí)現(xiàn)了promise接口恨统,可以調(diào)用promise內(nèi)的方法
3怪蔑,DeferredManager
DeferredManager dm = new DefaultDeferredManager();
DeferredManager dm = new DefaultDeferredManager(executorService);//默認(rèn)的ExecutorService是Executors.newCachedThreadPool();
AndroidDeferredManager dm = new AndroidDeferredManager();//針對(duì)android,AndroidDeferredManager 繼承了DefaultDeferredManager
核心方法:dm.when(params)
params有以下幾種方式:
Callable, Runnable, Future,
DeferredRunnable, DeferredCallable, DeferredFutureTask活翩,
DeferredAsyncTask
(中三種是上三種的子類(lèi)烹骨,內(nèi)部持有一個(gè)DeferredObject,上三種傳入后也會(huì)被封裝為DeferredFutureTask,內(nèi)部持有一個(gè)DeferredObject)
(DeferredFutureTask繼承于FutureTask(實(shí)現(xiàn)Runnable\Future,并持有Callable))
(DeferredAsyncTask繼承與AsyncTask,是android特有的材泄,內(nèi)部持有一個(gè)DeferredObject)
4,支持同步和異步
a,同步的使用:
Deferred deferred = new DeferredObject();
Promise promise = deferred.promise();
promise.done(new DoneCallback() {
public void onDone(Object result) {
...
}
}).fail(new FailCallback() {
public void onFail(Object rejection) {
...
}
}).progress(new ProgressCallback() {
public void onProgress(Object progress) {
...
}
}).always(new AlwaysCallback() {
public void onAlways(State state, Object result, Object rejection) {
...
}
});
deferred.resolve("done");
deferred.reject("oops");
deferred.notify("100%");
原理:DeferredObject內(nèi)部持有一個(gè)promise沮焕,當(dāng)調(diào)用resolve()時(shí),回調(diào)promise.done();調(diào)用reject()時(shí)脸爱,回調(diào)promise.fail()..以此類(lèi)推
或者
DeferredManager dm = new DefaultDeferredManager();
Promise p1, p2, p3;
// initialize p1, p2, p3
dm.when(p1, p2, p3)
.done(…)
.fail(…)
原理:DeferredManager內(nèi)部對(duì)promise的返回做了原子性處理遇汞,保證了線(xiàn)程安全
b,異步的使用:
三種用法:
第一種:線(xiàn)程中使用deferred
final Deferred deferred = ...
Promise promise = deferred.promise();
promise.then(…);
Runnable r = new Runnable() {
public void run() {
while (…) {
deferred.notify(myProgress);
}
deferred.resolve("done");
}
}
第二種:使用DeferredManager 和DeferredRunnable
DeferredManager dm = …;
dm.when(new DeferredRunnable<Double>(){
public void run() {
while (…) {
notify(myProgress);
}
}
}).then(…);
第三種:使用AndroidDeferredManager(內(nèi)部封裝了AsyncTask和handler)
AndroidDeferredManager dm = new AndroidDeferredManager();
dm.when(new DeferredAsyncTask<Void, Progress, Result>(){
@Override
protected Object doInBackgroundSafe(Void... voids) throws Exception {
return null;
}
}).then(...);