Gerbil Gerbil - 28 days ago 8
Swift Question

Querying for an object in a list in Realm

I'm using Realm 2.0 (Swift). My model is composed of

X
and
Y
(classes).
X
has a property called
list
which is a Realm List of
Y
objects.

Let's assume I have an instance of a
Y
object, called
y

The query I'm looking for is:


  • What X items contains an object equal to
    y
    in it's list of
    Y
    objects?



If the list was just a list of strings, I assume this would be trivial as the query would be:
realm.objects(X.self).filter("<string> IN list")


Thanks :-)

Answer

You can express this as either:

realm.objects(X.self).filter("%@ IN list", y)

or:

realm.objects(X.self).filter("ANY list = %@", y)