【问题标题】:Android Realm query inverse relationshipAndroid Realm 查询反向关系
【发布时间】:2019-08-19 03:31:13
【问题描述】:

使用 Person -> Realm 文档中的 Dogs 示例,我如何获得所有没有所有者的 Dogs?

class Person extends RealmObject {
    // ...
    private RealmList<Dog> dogs;
}

class Dog extends RealmObject {
    // ...
    @LinkingObjects("dogs")
    private final RealmResults<Person> owners = null;
}

【问题讨论】:

    标签: android realm inverse-relationship


    【解决方案1】:

    根据这里Queries的教程,可以这样查询Realm DB中的记录

    // Build the query looking at all users:
    RealmQuery<User> query = realm.where(User.class);
    
    // Add query conditions:
    query.equalTo("name", "John");
    query.or().equalTo("name", "Peter");
    
    // Execute the query:
    RealmResults<User> result1 = query.findAll();
    
    // Or alternatively do the same all at once (the "Fluent interface"):
    RealmResults<User> result2 = realm.where(User.class)
                                      .equalTo("name", "John")
                                      .or()
                                      .equalTo("name", "Peter")
                                      .findAll();
    

    您可以遍历结果 1 以查找拥有空主人的狗,或设置查询以查找拥有空主人的狗。

    例如这样:

    RealmResults<User> result2 = realm.where(Dog.class)
                                      .isNotEmpty("owners")
                                      .findAll();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-05-18
      • 2012-06-17
      相关资源
      最近更新 更多