简体   繁体   English

Android Realm查询逆向关系

[英]Android Realm query inverse relationship

Using Person -> Dogs example from Realm documentation, how do I obtain all Dogs without owner? 使用Realm文档中的Person-> Dogs示例,如何获得所有没有所有者的Dogs?

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

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

According to the tutorial here Queries , you can query the record in Realm DB like this 根据此处查询的教程,您可以像这样查询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();

You can either loop through result 1 to find Dogs with empty owners or set the query to find dogs with empty owners. 您可以遍历结果1来查找拥有者为空的狗,也可以设置查询以查找拥有者为空的狗。

Like this for example: 例如:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM