How to get a List from Flux without blocking?

前端 未结 1 1964
小鲜肉
小鲜肉 2021-01-23 04:16

I have a repository that returns a flux and wanted to set the result to another object which is expecting a list. Is there any other way to get the results as a list without blo

1条回答
  •  说谎
    说谎 (楼主)
    2021-01-23 04:55

    In short - NO. You don't need to extract List from Flux. If you've started use Reactor Streams - stay with it.

    Try this code:

    public void setUserPosts(User user) {
        postRepository.findAllByOrderId(user.getId())
            .collectList()
            .doOnNext(user::setPostList)// (1)
            .subscribe();               // (2) 
    }  
    
    1. if you set operation is blocking please use publishOn/subscribeOn to avoid blocking for the all stream.
    2. it starts your stream performing

    0 讨论(0)
提交回复
热议问题