Incognito
Incognito

Reputation: 493

Realm.io swift query with predicate on relation

I got a realm.io database that look like this

class A {
    dynamic var name = ""
    dynamic var obj: B
}

class B {
    dynamic var name = ""
}

So object A got a name and has a B object that has a name

What I'm trying to do is query all the A object that equal nameX and has the B.name IN ["nameY", "nameZ"] <==> (B.name = "nameY" OR b.name = "nameZ")

Here is what I got

var predicate: NSPredicate = NSPredicate(format: "name = '\(aName)' AND obj.name IN [\(bNameList)]")!
var result = A.objectsWithPredicate(predicate).sortedResultUsingProperty("name", ascending: true)

Was append is I got an error saying that the string can not be parsed.

'Unable to parse the format string "name = 'A50D26FC-04EF-4B8B-8AFC-ED5949E6453D' AND obj.name IN ['4A3CEEFC-35C0-4E1C-B5A9-7FA3B8FCD058', '53B0FBFD-54DD-4517-B9D9-8C19D6EB5E08']"'

Am I supposed to do it differently? I'm a little noob in swift. Thank for the help.

Upvotes: 1

Views: 1171

Answers (1)

segiddins
segiddins

Reputation: 4120

Let's try using NSPredicate's handy ability to substitute in values:

NSPredicate = NSPredicate(format: "name = %@ AND obj.name IN %@", aName, bNameList)!

Upvotes: 3

Related Questions