Pravin Tate
Pravin Tate

Reputation: 1130

Realm how to write subquery

I am study about Realm db, this db is nice as compare with core data but I am stuck on one place as follows:

I have two RLMObject in that I created relationship and I want to run join query (sub query) on that, but I can't do that.


first object (table) in Ralm

class Dog : RLMObject
{
    dynamic var name = ""
    dynamic var age = 0

    // create variable of Owner object 
    dynamic var owner = RLMArray(objectClassName: "Owner")

    override class func primaryKey() -> String!{
        return "name"
    }
}

second object (table) in Ralm

class Owner : RLMObject{
    dynamic var myName = ""
}

so I want to fetch only those Dog names which belong with owner name 'ram' I tried following sub query

var dog = Dog.allObjects().objectsWithPredicate(NSPredicate(format:"SUBQUERY(Owner, $owner, $owner.myName = 'ram')", argumentArray: nil))

but app is crashing with following error

RealTest[1701:17960] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Unable to parse the format string "SUBQUERY(owner, $owner, $owner.myName = 'ram')"'

also I search it on net I found that realm.objects but it gave me error about not found. enter image description here

Thanks in advance!

Upvotes: 5

Views: 5638

Answers (3)

Sir Wellington
Sir Wellington

Reputation: 596

Your predicate should look like this:

let predicate = NSPredicate(format:"SUBQUERY(Owner, $owner, $owner.myName = 'ram') .@count > 0", argumentArray: nil)

The idea here is to make sure you add .@ count > 0 at the end, as the predicate needs to return true or false for it to work.

Upvotes: 5

bdash
bdash

Reputation: 18308

This can be achieved using a query like:

Dog.allObjects().objectsWhere("ANY owner.myName = 'ram'")

SUBQUERY is only necessary if you have multiple constraints on the target of the relationship that must all be fulfilled by a single row, or if you wish to express a constraint other than ANY / ALL / NONE on the number of rows that match.

That said, as of Realm Objective-C and Swift 0.98.0 SUBQUERY is now supported.

Upvotes: 4

TiM
TiM

Reputation: 15991

While Realm supports filtering objects via NSPredicate, at this time of writing, Realm's implementation of NSPredicate yet support every single type of keyword that the native Apple frameworks do, including SUBQUERY. Realm provides an NSPredicate cheat sheet on its website, outlining which types of queries it presently supports.

That being said, if you already have an Owner object at this point, you can actually use another Realm method on the Owner object ([RLMObject linkingObjectsOfClass:forProperty:]) to find out which Dog objects are referencing it.

Finally, that realm.objects error is because that syntax is from the native Swift version of Realm, and the code you're using here is the Objective-C version of Realm, bridged over to Swift.

Let me know if you need any more clarification!

Upvotes: 0

Related Questions