Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Realm: sorting by property in child object

Tags:

ios

swift

realm

My Show object looks like this:

class Show: RLMObject {
    dynamic var venue: Venue?
}

and my Venue object:

class Venue: RLMObject {
    dynamic var title = ""
}

I need to be able to sort my Show objects by their Venue object's titles. I tried the following but got an error:

allShowsByLocation = Show.allObjects().sortedResultsUsingProperty("venue.title", ascending: true)

The error is: Invalid sort column', reason: 'Column named '(null)' not found.

like image 950
codeman Avatar asked Dec 08 '14 21:12

codeman


1 Answers

Realm doesn't yet support sorting RLMResults by a sub-property. As a work-around, you could query for Venues and return its linking object for each index:

allVenues = Venue.allObjects().sortedResultsUsingProperty("title", ascending: true)

func showAtIndex(index: UInt) -> Show {
  return (allVenues[index] as Venue).linkingObjectsOfClass("Show", forProperty: "venue")
}

Or you could simply add a venueTitle property to your Show model which would then allow your query to work:

allShowsByLocation = Show.allObjects().sortedResultsUsingProperty("venueTitle", ascending: true)

You can also subscribe to GitHub issue #1199 to follow our progress on supporting sub-property sorting.

like image 113
jpsim Avatar answered Sep 28 '22 10:09

jpsim