Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

flow写法同时请求多个接口怎么处理呢 #12

Open
sceneren opened this issue Dec 14, 2021 · 1 comment
Open

flow写法同时请求多个接口怎么处理呢 #12

sceneren opened this issue Dec 14, 2021 · 1 comment

Comments

@sceneren
Copy link

No description provided.

@ruirui1128
Copy link

这个需要根据实际业务进行处理,比如说zip三个请求,如果其中一个业务code不正确,是忽略其中一个还是整体不成立,由业务觉决定。

fun request() = flow { emit(apiService.request()) }
fun request2() = flow { emit(apiService.request2()) }
fun request3() = flow { emit(apiService.request3()) }

 viewModelScope.launch {
     request()
         .zip(request()2){ r1,r2->{
                   val list= mutableListOf<String>()           // 如果request() 返回code不成功 整体抛异常
                    if(r1.code ==0){
                      list.addAll(r1.data)
                 }else{
                  throw nullExcpt
               }
                 if(r2.code==0){                                     // 如果request2()返回code不成功,则忽略
                  list.addAll(r2.data)
        }

                    return@zip list
       }
         .zip(request()3){ r12,r3->
        }
       .catch{ 
       }
       .collect {
                    }
   }

}


Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants