Linked query for contains purpose

17 views
Skip to first unread message

Mohit Singh Kanwal

unread,
Oct 21, 2015, 1:01:48 AM10/21/15
to Realm Java
Hi,

Just wanted to check if this approach is correct in terms of doing a linked query.

class Student {
    private int id;
}

class StudentCouncil {
    private RealmList<Student> members;
}

For  a student with id = 12, is this correct?

RealmQuery<StudentCouncil> query = realm.where(StudentCouncil.class)
                                   .equalTo("members.id", 12).findAll() // returns 2 rows

However, to my surprise I find that 

RealmQuery<StudentCouncil> query = realm.where(StudentCouncil.class)
                                        .notEqualTo("members.id", 12).findAll() // returns 3 rows

There are 3 councils and student is a member of only 2, and not a member of only 1 council. Is this correct? Or a I missing something.

Realm Version - 0.83.1

Regards
Mohit

Kenneth Geisshirt

unread,
Oct 21, 2015, 1:56:15 PM10/21/15
to Mohit Singh Kanwal, realm...@googlegroups.com
Hi,

Link queries can be a bit tricky. Say you have:

1. four students: 0:id=11, 1:id=12, 2:id=13, 3:id=14
2. three councils: 0: {0, 1}, 1: {0, 2}, 2:{1, 3}

The query realm.where(StudentCouncil.class).notEqualTo("members.id", 12) should be read as "give me all StudentCouncils with at least one member with id != 12". As all three councils have at least a number with id != 12 (council 0: both, council 1: both, council: one), the three council objects are returned.

In your case, you are probably better off by doing:

for (StudentCouncil sc : realm.allObjects(StudentCouncil) {
if (sc.getMembers().where().equalTo("id", 12).count() == 0) {
// student 12 not member - hurrah!
}
}

Best,
Kenneth

--
Kenneth Geisshirt
Member of technical staff


--
You received this message because you are subscribed to the Google Groups "Realm Java" group.
To unsubscribe from this group and stop receiving emails from it, send an email to realm-java+...@googlegroups.com.
To post to this group, send email to realm...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/realm-java/77cca93c-f172-4099-9e87-12b9d72df5a4%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



{#HS:131987769-2096#}
Reply all
Reply to author
Forward
0 new messages