android中怎么利用Flow实现流式响应
创始人
2024-12-20 06:25:40
0次

简单使用

通过Flow 静态create方法创建一个流,then串联下个流,如果不需要返回Void泛型。Event有两个泛型P、R,第一个是前个流Flow的返回值类型,第二个是当前流Flow返回类型。await exec方法是结束当前事件流,并将结果代入下个流。

打印两句话

Flow.create(new Event() {          @Override          public void run(Flow flow, Void aVoid, Await await) {            System.out.println("this is first flow");            await.exec(null);          }        }).then(new Event() {          @Override          public void run(Flow flow, Void aVoid, Await await) {            System.out.println("this is two flow");            await.exec(null);          }        }).start();

Lambda简化之后

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();

两数相加

Flow.create((FirstEvent) (flow, await) ->            await.exec(3))           .then((Event) (flow, integer, await) ->               await.exec(integer + 5))           .resultThen((flow, result) ->               System.out.println("total is"+result))           .start();

resultThen方法返回是当前流的结果,每个flow后面使用resultThen都可以获取流的结果。如果遇到异常,可以通过flow throwException方法抛出,可以在flow后面catchThen立刻处理,也可以在最后flow catchThen处理。finallyThen是事件流结束一个通知。

Flow.create((FirstEvent) (flow, await) ->            await.exec(0))           .then((Event) (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传递一个Converter参数,代表下个流切换。如果两个Converter参数,代表当前流和下个流都切换线程。当然你也可以实现Converter接口来实现其他功能。

Flow.create((FirstEvent) (flow, await) ->            await.exec(0))           .on(AndroidMain.get(),SingleThread.get())           .then((Event) (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结果转换成多个流

Flow.each((FirstEvent>) (flow, await) -> {          ArrayList list = new ArrayList<>();          list.add("1");          list.add("2");          list.add("3");          await.exec(list);        }).then((LastEvent) (flow, s, await) -> {          System.out.println("this is"+s);        }).start();

多个流结果转换成一个流

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();

条件选择

根据条件判断重新发起Flow流(返回参数可以不一样)

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();

根据条件判断执行Flow流,可以合并到一起。(返回参数必须一致)

Flow.condition2(() -> isGo, (FirstEvent) (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();

生命周期解绑

通过flow watch方法。被观察者必须实现ILifeObservable接口。

Flow.create((FirstEvent) (flow, await) ->await.exec(0))   .watch(this).start();

相关内容

热门资讯

空调连续工作多久需要停机休息 空调连续工作的时间取决于多个因素,包括空调的型号、功率、使用环境和维护情况等。一般来说,空调可以连续...
空调哪个符号是除湿的 空调除湿功能通常用一个水滴形状的符号表示。不同品牌和型号的空调遥控器可能会有所差异,但一般来说,除湿...
格力空调多长时间加一次氟利昂 1、一般情况下,空调对氟利昂的使用量是非常小的,所以在空调使用了四年左右的时候再考虑加也没问题。2、...
空调用多长时间需要加氟一次 一般情况下是五年。当出现以下这几种情况时,空调才需要加氟。1、空调的室内机和室外机各有两个接口,当接...
格调生活·C标思享荟:卡萨帝在... 9月12日的郑州龙湖金融岛,夜风轻拂,灯光璀璨。“C标时刻共传奇——卡萨帝思享荟暨20周年品牌盛典郑...
品牌价值破千亿、高端份额持续领... 9月12日,“C标时刻共传奇——卡萨帝思享荟暨20周年品牌盛典郑州站”在郑州龙湖金融岛举办。2026...
空调主板坏了修800还是换新的 空调主板坏了是修还是换新,需要考虑多个因素,以下是一些建议:1. 维修成本:如果维修费用较低,比如 ...
空调开多久需要休息多长时间 问题:空调开多久需要休息多长时间回答:1、在使用空调的过程中,一般空调开7~8个小时左右需要休息2~...
中央空调多长时间清洗一次 1、一般空调开机前清洗一次即可,空调开机中间时段再清洗一次,空调关机时清洗一次,这样比较合理。2、空...
中央空调氟利昂多久加一次 看空调制热和制冷程度,很多人的观点认为氟利昂必须一年放一次,其实这个观点是错误的,理解上面有点偏差,...