title: How can I perform a subquery in Core Data for to-many relationship?
body: <p>I did some research and cannot find the answer to a simple Core Data issue.</p>
  
  <p>Here is the simplified object model:</p>
  
  <pre><code>Person --&gt;&gt; Events
  Event --&gt; Date
  </code></pre>
  
  <p>What I would like to do is query all Persons and sort them by the most recent event.</p>
  
  <p>I took a look at some postings on SUBQUERY within a predicate.  However, I don't know how to query for the most recent.  I can't use "event.date" in the Sort Descriptor as it is not allowed since it is an NSSet.</p>
  
  <p>Some code for reference...</p>
  
  <pre><code>NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
  NSEntityDescription *entity = [NSEntityDescription entityForName:@"Person" inManagedObjectContext:self.managedObjectContext];
  [fetchRequest setEntity:entity];
  
  // The following errors:    
  NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"event.date" ascending:NO selector:nil];
  </code></pre>
  
  <p>Any help is appreciated.</p>