Best practice for sorting by back- linked object properties

124 views
Skip to first unread message

Tom

unread,
Oct 13, 2015, 3:49:20 PM10/13/15
to Realm
It might be a newbie question. What are the best practices or is it even a good practice at all to try and access back linked properties for sorting objects. For example on my model I have a Farmer(), a Barn() an Horse().

Horse() has Farmer() property and Barn() as property

By using back links(e.g farmer.horses ) I can totally avoid data duplication. The problem arises when I want to sort the all Farmers by whe oldest Horse() they have. Because one farmer can have multiple horses I cannot have a Horse() as a property.

As Realm does not yet support predicate queries by properties of linked objects I am kind of stuck.

Any ideas how to sort or modify the model?

Marius Rackwitz

unread,
Oct 13, 2015, 4:41:35 PM10/13/15
to Tom, realm...@googlegroups.com
It is currently not possible to access back linked properties to sort objects.
In your case, supposing you have just backlinks to navigate, you would need to retrieve every single Farmer, query for their oldest Horse, memorize its age together with the Farmer or a candidate index / primary key and then you can sort the list in memory and finally retrieve Farmers by the index again, if necessary.
But even if you would have links, e.g. by inverting the direction, you would still not be able currently to query directly for the Farmer with the oldest Horse, because that would require aggregate queries on links, which we don't support yet. We track adding this feature by issue #1166.

--
Marius Rackwitz
iOS Product Engineer


--
You received this message because you are subscribed to the Google Groups "Realm" group.
To unsubscribe from this group and stop receiving emails from it, send an email to realm-cocoa...@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-cocoa/00570283-09b7-4978-ac4c-f583c09cf338%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



{#HS:130122130-2043#}

Alexander Stigsen

unread,
Oct 13, 2015, 4:55:53 PM10/13/15
to Tom, realm...@googlegroups.com
Because one farmer can have multiple horses I cannot have a Horse() as a property.

Couldn't you just make the Farmer class have a list member containing all his horses? Then you wouldn't need to use backlinks at all.

Tom

unread,
Oct 14, 2015, 6:38:52 AM10/14/15
to Realm, to...@kamatom.com
I could but this would make the data model less clean and still not solve the problem : being able to get  all farmers sorted by the age of their oldest horse with a realm query. 
I have solved the problem with a couple of simple helper methods but I guess once Realm implements that functionality i will love it even more.

Marius Rackwitz

unread,
Oct 15, 2015, 9:52:18 AM10/15/15
to Tom, realm...@googlegroups.com
Good to hear that you found a workaround.
For the sake of completeness, we track the feature of querying backlinks by issue #1324.


--
Marius Rackwitz
iOS Product Engineer


{#HS:130122130-2043#}
Reply all
Reply to author
Forward
0 new messages