您好,登錄后才能下訂單哦!
android中怎么利用Flow實(shí)現(xiàn)流式響應(yīng),針對(duì)這個(gè)問(wèn)題,這篇文章詳細(xì)介紹了相對(duì)應(yīng)的分析和解答,希望可以幫助更多想解決這個(gè)問(wèn)題的小伙伴找到更簡(jiǎn)單易行的方法。
簡(jiǎn)單使用
通過(guò)Flow 靜態(tài)create方法創(chuàng)建一個(gè)流,then串聯(lián)下個(gè)流,如果不需要返回Void泛型。Event有兩個(gè)泛型P、R,第一個(gè)是前個(gè)流Flow的返回值類(lèi)型,第二個(gè)是當(dāng)前流Flow返回類(lèi)型。await exec方法是結(jié)束當(dāng)前事件流,并將結(jié)果代入下個(gè)流。
打印兩句話
Flow.create(new Event<Void,Void>() { @Override public void run(Flow flow, Void aVoid, Await<Void> await) { System.out.println("this is first flow"); await.exec(null); } }).then(new Event<Void, Void>() { @Override public void run(Flow flow, Void aVoid, Await<Void> await) { System.out.println("this is two flow"); await.exec(null); } }).start();
Lambda簡(jiǎn)化之后
Flow.create((NoneEvent) (flow, await) -> { System.out.println("this is first flow"); await.exec(); }).then((NoneEvent) (flow, await) -> { System.out.println("this is two flow"); await.exec(); }).start();
兩數(shù)相加
Flow.create((FirstEvent<Integer>) (flow, await) -> await.exec(3)) .then((Event<Integer, Integer>) (flow, integer, await) -> await.exec(integer + 5)) .resultThen((flow, result) -> System.out.println("total is"+result)) .start();
resultThen方法返回是當(dāng)前流的結(jié)果,每個(gè)flow后面使用resultThen都可以獲取流的結(jié)果。如果遇到異常,可以通過(guò)flow throwException方法拋出,可以在flow后面catchThen立刻處理,也可以在最后flow catchThen處理。finallyThen是事件流結(jié)束一個(gè)通知。
Flow.create((FirstEvent<Integer>) (flow, await) -> await.exec(0)) .then((Event<Integer, Integer>) (flow, perVal, await) ->{ if(perVal == 0){ flow.throwException("Dividend cannot be 0!"); }else{ await.exec(perVal/5); } }) .resultThen((flow, result) -> System.out.println("total is"+result)) .catchThen((flow, e) -> System.out.println(e.getMessage())) .finallyThen((flow, await) -> System.out.println("this is flow end")).start();
切換線程
使用flow on方法可以切換線程,on傳遞一個(gè)Converter參數(shù),代表下個(gè)流切換。如果兩個(gè)Converter參數(shù),代表當(dāng)前流和下個(gè)流都切換線程。當(dāng)然你也可以實(shí)現(xiàn)Converter接口來(lái)實(shí)現(xiàn)其他功能。
Flow.create((FirstEvent<Integer>) (flow, await) -> await.exec(0)) .on(AndroidMain.get(),SingleThread.get()) .then((Event<Integer, Integer>) (flow, perVal, await) ->{ if(perVal == 0){ flow.throwException("Dividend cannot be 0!"); }else{ await.exec(perVal/5); } }) .on(AndroidMain.get()) .resultThen((flow, result) -> System.out.println("total is"+result)) .on(AndroidMain.get()) .catchThen((flow, e) -> System.out.println(e.getMessage())) .on(SingleThread.get()) .finallyThen((flow, await) -> System.out.println("this is flow end")).start();
Collection結(jié)果轉(zhuǎn)換成多個(gè)流
Flow.each((FirstEvent<List<String>>) (flow, await) -> { ArrayList<String> list = new ArrayList<>(); list.add("1"); list.add("2"); list.add("3"); await.exec(list); }).then((LastEvent<String>) (flow, s, await) -> { System.out.println("this is"+s); }).start();
多個(gè)流結(jié)果轉(zhuǎn)換成一個(gè)流
Flow.merge((flow, await) -> await.exec(1), (flow, await) -> await.exec(2), (flow, await) -> await.exec(2)).resultThen((flow, result) -> System.out.println"result"+result)).start();
條件選擇
根據(jù)條件判斷重新發(fā)起Flow流(返回參數(shù)可以不一樣)
Flow.create((NoneEvent) (flow,await) ->{ System.out.println("start"); await.exec(); }) .on(SingleThread.get()) .conditionThen((VoidCondition) () -> false, Flow.create((NoneEvent) (flow,await) -> { System.out.println("this is true"); await.exec(); }), Flow.create((NoneEvent) (flow,await) -> { System.out.println("this is false"); await.exec(); })).start();
根據(jù)條件判斷執(zhí)行Flow流,可以合并到一起。(返回參數(shù)必須一致)
Flow.condition2(() -> isGo, (FirstEvent<Integer>) (flow, await) -> { System.out.println("this is true"); await.exec(1); }, (flow, await) -> { System.out.println("this is false"); await.exec(0); }).resultThen((flow, result) -> System.out.println("result"+result)) .watch(this).start();
生命周期解綁
通過(guò)flow watch方法。被觀察者必須實(shí)現(xiàn)ILifeObservable接口。
Flow.create((FirstEvent<Integer>) (flow, await) ->await.exec(0)) .watch(this).start();
關(guān)于android中怎么利用Flow實(shí)現(xiàn)流式響應(yīng)問(wèn)題的解答就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,如果你還有很多疑惑沒(méi)有解開(kāi),可以關(guān)注億速云行業(yè)資訊頻道了解更多相關(guān)知識(shí)。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。