温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

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

发布时间:2021-08-10 17:12:34 来源:亿速云 阅读:155 作者:Leah 栏目:编程语言

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

简单使用

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

打印两句话

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简化之后

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

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传递一个Converter参数,代表下个流切换。如果两个Converter参数,代表当前流和下个流都切换线程。当然你也可以实现Converter接口来实现其他功能。

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结果转换成多个流

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

多个流结果转换成一个流

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

生命周期解绑

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

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

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

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI