资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

android中怎么利用Flow实现流式响应

android中怎么利用Flow实现流式响应,针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。

创新互联专业IDC数据服务器托管提供商,专业提供成都服务器托管,服务器租用,郑州服务器托管郑州服务器托管,成都多线服务器托管等服务器托管服务。

简单使用

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

关于android中怎么利用Flow实现流式响应问题的解答就分享到这里了,希望以上内容可以对大家有一定的帮助,如果你还有很多疑惑没有解开,可以关注创新互联行业资讯频道了解更多相关知识。


网站标题:android中怎么利用Flow实现流式响应
本文来源:http://cdkjz.cn/article/igdhjj.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

大客户专线   成都:13518219792   座机:028-86922220