【问题标题】:Observable from iterable doesn't print all elements可迭代的 Observable 不会打印所有元素
【发布时间】:2018-10-29 15:38:05
【问题描述】:

我在 Rest Web 服务中使用 RxJava2 Observable.fromIterable()。 我的可迭代示例由三个元素组成,但我的非阻塞休息服务仅返回三个元素中的一个。

class ToDoDaoImpl implements ToDoDao {
  Map<String, ToDo> toDos;
  ...
  public Observable<ToDo> readAll() {
    return Observable.fromIterable(toDos.entrySet().stream().map(entry -> entry.getValue()).collect(Collectors.toList()));
  }
}

当我从我的 Non-Blocking Rest 库中调用 readAll() 方法时,我只能在三个上得到一个元素:

@Api(path = "/api/v2/read", method = "GET", produces = "application/json")
Action readAllToDos = (HttpServletRequest request, HttpServletResponse response) -> {
    Observable.just(request)
            .flatMap(req -> toDoDao.readAll())
            .subscribe(output  -> toJsonResponse(request, response, new ResponseDto(200, output)),
                       error   -> toJsonResponse(request, response, new ResponseDto(200, error))
            );
};

我的输出:

{
"status": 200,
"response": {
    "id": "5dc74dd8-1ea9-427e-8bb7-482cc6e24c71",
    "title": "learn ReactiveJ",
    "description": "learn to use ReactiveJ library",
    "date": {
        "year": 2018,
        "month": 10,
        "day": 29
    }
},
"datetime": "Oct 29, 2018 4:19:51 PM"
}

如果我调用我的 Dao 的 Non-reactive 等价物,我会得到我所期望的:

{
"status": 200,
"response": [
    {
        "id": "25cbe3bf-12be-42e4-82ce-d4780f6469f6",
        "title": "study reactive",
        "description": "learn reactive programming",
        "date": {
            "year": 2018,
            "month": 10,
            "day": 29
        }
    },
    {
        "id": "51879241-f005-43fa-80fb-78386b663cb7",
        "title": "learn ReactiveJ",
        "description": "learn to use ReactiveJ library",
        "date": {
            "year": 2018,
            "month": 10,
            "day": 29
        }
    },
    {
        "id": "80a07c1b-2317-4eb8-9a39-ac35260f37a2",
        "title": "exercise",
        "description": "do some exercises",
        "date": {
            "year": 2018,
            "month": 10,
            "day": 29
        }
    }
],
"datetime": "Oct 29, 2018 4:37:05 PM"
}

【问题讨论】:

    标签: java observable reactive-programming rx-java2


    【解决方案1】:

    如果你在订阅之前输入doOnNext,你会看到你得到了多个元素,但显然 toJsonResponse 只能传递一个。我敢打赌,您的非响应式版本只是将整个 List 传递给 ResponseDto

    我不知道你为什么把任务复杂化了,但这应该可行:

    class ToDoDaoImpl implements ToDoDao {
        Map<String, ToDo> toDos;
        // ...
        public Observable<List<ToDo>> readAll() {
            return Observable.fromCallable(() -> new ArrayList<>(toDos.values()));
        }
    }
    
    @Api(path = "/api/v2/read", method = "GET", produces = "application/json")
    Action readAllToDos = (HttpServletRequest request, HttpServletResponse response) -> 
    {
        toDoDao.readAll()
            .subscribe((List<ToDo output)  -> 
                toJsonResponse(request, response, new ResponseDto(200, output)),
                       error   -> 
                toJsonResponse(request, response, new ResponseDto(200, error))
            );
    };
    

    【讨论】:

      猜你喜欢
      • 2023-02-22
      • 2023-01-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-05
      • 2019-05-04
      • 1970-01-01
      相关资源
      最近更新 更多