从Flowable Room ORM发射每个物品 [英] Emit each item from Flowable Room ORM

查看:109
本文介绍了从Flowable Room ORM发射每个物品的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在房间ORM"中有一个项目列表,希望在回收者"视图中显示.数据正在从网络添加到数据库.问题是我每次都从Flowable而不是每个项目发出整个列表.我尝试使用.distinctUntilChanged进行了相同的操作.

I have a list of items in the Room ORM which I would like to display in a Recycler view. The data is being added from the network to the db. The problem is I am getting every time the whole list emited from the Flowable and not each item. I have tried with .distinctUntilChanged with no difference.

@Query("SELECT * FROM items")
Flowable<List<Item>> getItems();

我还尝试过仅返回单个项目,该项目仅加载第一个数据库.

I have tried also to return only a single item which loads only the first one that is the db.

推荐答案

您可以使用flatMap获取项目流.

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.

    });

如果您只需要第一项:

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.

        });

这篇关于从Flowable Room ORM发射每个物品的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆