title: How to define a NSPredicate which specifies a having max(x) clause using group by?
body: <p>I am having trouble creating a valid NSPredicate within an iOS-CoreData context. The table contains all messages of all buddies. I need to get the newest message of each buddy:</p>
  
  <p>I have tried using DBVisualizer and I need the following SQL (which works fine) </p>
  
  <pre><code>    /* for testing with a db tool like DbVisualizer
       select ZBAREJIDSTR,ZBODY, ZTIMESTAMP
       from ZXMPPMESSAGEARCHIVING_MESSAGE_COREDATAOBJECT
       group by ZBAREJIDSTR
       having max(ZTIMESTAMP)
       order by ZTIMESTAMP desc;
       */
  </code></pre>
  
  <p>The following code fails with "Unable to parse the format string" and I could not find an example which worked for me. </p>
  
  <pre><code>    - (NSFetchedResultsController *)fetchedResultsController {
  if (fetchedResultsController == nil)    {
  
      NSManagedObjectContext *moc = [[Utilities chatManagerFromAppDelegate] managedObjectContext_messageArchiving];
  
      NSEntityDescription *entity = [NSEntityDescription entityForName:@"XMPPMessageArchiving_Message_CoreDataObject"
                                                inManagedObjectContext:moc];
  
      NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
      NSAttributeDescription* bodyDesc = [entity.attributesByName objectForKey:@"body"];
      NSAttributeDescription* barJidStrDesc = [entity.attributesByName objectForKey:@"barJidStr"];
      NSAttributeDescription* timestampDesc = [entity.attributesByName objectForKey:@"timestamp"];
  
      NSPredicate *predicateMaxTimestamp = [NSPredicate predicateWithFormat:@"@max.timestamp"];
  
      NSSortDescriptor *sortDescriptorTimestampDesc = [[NSSortDescriptor alloc] initWithKey:@"timestamp" ascending:NO];
      NSArray *sortDescriptors = [NSArray arrayWithObjects:sortDescriptorTimestampDesc, nil];
  
      [fetchRequest setEntity:entity];
      [fetchRequest setPropertiesToFetch:[NSArray arrayWithObjects:barJidStrDesc, bodyDesc, timestampDesc, nil]];
      [fetchRequest setPropertiesToGroupBy:[NSArray arrayWithObject:bodyDesc]];
      [fetchRequest setHavingPredicate: predicateMaxTimestamp];
      [fetchRequest setSortDescriptors:sortDescriptors];
      [fetchRequest setResultType:NSDictionaryResultType];
      [fetchRequest setFetchBatchSize:10];
      fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest
                                                                     managedObjectContext:moc
                                                                       sectionNameKeyPath:nil
                                                                                cacheName:nil];
      ....
  </code></pre>
  
  <p>Actually I would also appreciate other alternatives, because I need updates of the underlaying model ([fetchedResultsController setDelegate:self];) which is not working with a NSDictionaryResultType (but NSDictionaryResultType is necessary for with a group by).</p>