Emit each item from Flowable Room ORM

淺唱寂寞╮ 提交于 2019-12-04 13:02:09

You can use flatMap to get stream of items.

itemDao.getItems().flatMap(list -> {
      Item[] items = new Item[list.size()];
      list.toArray(items);
      return Flowable.fromArray(items);
    }).subscribe(item -> {
      // Now you can do with each item.

    });

If you only need the first item:

itemDao.getItems().flatMap(list -> {
          Item[] items = new Item[list.size()];
          list.toArray(items);
          return Flowable.fromArray(items);
        })
         .firstElement()
         .subscribe(first -> {
          // Now you can do with the first one.

        });

Yes, Flowable<List<Item>> means you'll get a single callback when the list changes: this is how Room works. Generally, you'd pass that list into DiffUtil, which then generates the set of changes needed to update your RecyclerView.

You can use flatMap and Flowable.fromIterable() to map to Flowable which will emit all items one by one

getItems()
    .flatMap(Flowable::fromIterable)
    .subscribe(item -> {

    });

That's it. Short and clean code, without casting toArray

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!