title: How to build a complex query with CoreData?
body: <p>I am new to iOS development and coming from a Java world I am completely lost on how I should go about querying a CoreData model.</p>
  
  <p>I am trying to show a list of messages grouped by who they are to and then ordered by the last message and whether the message is read or not. For example on Android I would run the following sql query.</p>
  
  <pre><code>SELECT *, wm1.date_timestamp AS TimeSpent " +
  "FROM messages wm1 " +
  "JOIN ( " +
  "SELECT *, MAX(date_timestamp) as dt " +
  "FROM messages " +
  "WHERE to_user = ?" +
  " GROUP BY from_user " +
  ") wm2 ON wm1.from_user = wm2.from_user AND wm1.date_timestamp = wm2.dt " +
  "ORDER BY wm1.is_read ASC, wm1.date_timestamp DESC
  </code></pre>
  
  <p>Can someone point me in the right direction on how to go about producing something similar on iOS.</p>