【问题标题】:Query a RealmList<Integer> with a parameter使用参数查询 RealmList<Integer>
【发布时间】:2018-05-01 12:43:59
【问题描述】:

我的模型是一个类别,它有一个父 ID 的列表。

private RealmList<Integer> parentCategories = new RealmList<>();

我需要给定类别的子类别。

我的查询如下所示:

Integer[] intArray = new Integer[1];
intArray[0] = category.getId();

Realm realm = Realm.getDefaultInstance();
        return realm.where(Category.class)
                .contains("parentCategories", category.getParentCategories())
                .findAll();

我也试过这个:

Realm realm = Realm.getDefaultInstance();
RealmQuery<Category> query = realm.where(Category.class);
for (Integer id : category.getParentCategories()) {
     query.or().equalTo("id", id);
}
RealmResults<Category> results = query.findAll();

但两者都不起作用。 你能指出我正确的方向吗?

更新:RealmQuery 尚不支持我正在尝试做的事情。 感谢您指出这一点!

【问题讨论】:

  • category的id和parentcategory一样吗?
  • 为什么parentCategories 不是RealmList&lt;Category&gt;? RealmQuery 尚不支持您尝试执行的操作。
  • 因为我只是从后端得到一个整数列表,而不是完整的对象......

标签: android realm realm-list


【解决方案1】:

如果要查询对象列表变量需要使用“in”-

String[] parentCategoriesArray = new String[]{"example"};

Realm realm = Realm.getDefaultInstance();
    return realm.where(Category.class)
            .in("parentCategories", parentCategoriesArray)
            .findAll();

【讨论】:

  • ID 是一个整数。我得到这个异常:java.lang.IllegalArgumentException:无效查询:类“Category”中的字段“parentCategories”是无效类型“INTEGER_LIST”。
  • object list variables 是的,但原语不是对象。所以不幸的是,这还不支持
  • @EpicPandaForce 是对的。你现在只能使用对象
  • @EpicPandaForce @nivm 这里的解决方法是什么?返回查询RealmList&lt;RealmInteger&gt;?
  • 我的例子,返回具有RealmList&lt;Integer&gt; 的RealmObjects 包含至少一个整数数组的值。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多