title: Core data sorting a Thread by latest Message (to many relationship sorting)
body: <p>My question, I believe, is fairly simple but I can't seem to find a decent and elegant solution to it. Maybe Core Data doesn't have an elegant solution or my Google skills have let me down, in which case I apologize.</p>
  
  <p>Lets say I have a simple messaging application. I have two Core Data entities: <code>Thread</code> and <code>Message</code>. A <code>Thread</code> contains many <code>Messages</code>.</p>
  
  <p>My application should open with a UITableView in which all the <code>Threads</code> are listed. The application will use RestKit and its object mapping mechanisme to load the data. The <code>Messages</code> have a <code>created</code> property.</p>
  
  <p>So I want to sort my list of Threads so that the Thread with the latest Messages appears on top and the thread with the oldest messages in it will apear on bottom.</p>
  
  <p>I believe this is also what happens in Apple's own messaging application, and it uses Core Data as well. This is the main reason I hope that there is a more elegant solution than for example store an extra to one relation to the latest message in the database. Which is in my case unpractical because I use RestKit object mapping and don't have influence on the HTTP API. </p>
  
  <p>I've tried transient values, this doesn't work because you can't sort on them. I have tried a sort descriptor with a NSComparisonResult block, but this also doesn't work because core data says it can't sort on to-many relationships. And I've looked into fetched properties, but I can't figure out what predicate to use.</p>
  
  <p>Thanks in advance.</p>