title: iOS : How to fetch objects with maximum count in core data?
body: <p>I've two classes in my core data model, and its one to many relationship. </p>
  
  <p>Lets take <strong>Class Album &lt;--->> Class Songs</strong> for the explanation. Now I want to fetch (top) five albums from <code>Album</code> which have the maximum no.of <code>Songs</code>.</p>
  
  <pre><code>Class Album
  {
      albumID (Integer)
      name    (String)
      songs   (class B)
  }
  
  Class Songs
  {
      name   (String)
      length (Integer)
  }
  </code></pre>
  
  <hr>
  
  <pre><code>@class Songs;
  @interface Album : NSManagedObject
  
  @property (nonatomic, retain) NSNumber * albumID;
  @property (nonatomic, retain) NSString * name;
  @property (nonatomic, retain) NSSet *songs;
  
  @end
  
  @interface Album (CoreDataGeneratedAccessors)
  - (void)addSongsObject:(Songs *)value;
  - (void)removeSongsObject:(Songs *)value;
  - (void)addSongs:(NSSet *)values;
  - (void)removeSongs:(NSSet *)values;
  @end
  </code></pre>
  
  <hr>
  
  <pre><code>@class Album;
  
  @interface Songs : NSManagedObject
  
  @property (nonatomic, retain) NSString * name;
  @property (nonatomic, retain) NSNumber * length;
  @property (nonatomic, retain) Album *owner;
  
  @end
  </code></pre>
  
  <hr>
  
  <p>Here's what I'm trying,</p>
  
  <pre><code>NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
  [fetchRequest setEntity:[NSEntityDescription entityForName:@"Albums" inManagedObjectContext:[self context]]];
  fetchRequest.fetchOffset = 0;
  fetchRequest.fetchLimit = 5;
  fetchRequest.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"albumID" ascending:YES]];
  fetchRequest.predicate = [NSPredicate predicateWithFormat:@"SELF @max.@count.songs"];
  NSError *error = nil;
  NSArray *albums = [[self context] executeFetchRequest:fetchRequest error:&amp;error];
  </code></pre>
  
  <p>If I don't pass predicate, it works fine. But problem is with predicate. It crash the app with following message: </p>
  
  <blockquote>
    <p>*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Unable to parse the format
    string "SELF @max.@count.songs"'</p>
  </blockquote>
  
  <p><strong>Update</strong>: I'm trying the same with <code>NSExpression</code> but not able to execute <code>max:</code> and <code>count:</code> functions together? below is what I've tried,</p>
  
  <pre><code>NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
  [fetchRequest setEntity:[NSEntityDescription entityForName:@"Albums" inManagedObjectContext:[self context]]];
  
  fetchRequest.fetchOffset = 0;
  fetchRequest.fetchLimit = 5;
  
  NSError *error = nil;
  
  NSExpression *keyPathExpression = [NSExpression expressionForKeyPath:@"songs"];
  NSExpression *countExpression = [NSExpression expressionForFunction:@"count:" arguments:[NSArray arrayWithObject:keyPathExpression]];
  //NSExpression *maxExpression = [NSExpression expressionForFunction:@"max:" arguments:[NSArray arrayWithObject:keyPathExpression]];
  
  NSExpressionDescription *maxCountExpressionDescription = [[NSExpressionDescription alloc] init];
  [maxCountExpressionDescription setName:@"squishes"];
  [maxCountExpressionDescription setExpression:countExpression];
  
  [fetchRequest setPropertiesToFetch:[NSArray arrayWithObject:maxCountExpressionDescription]];
  [fetchRequest setResultType:NSDictionaryResultType];
  
  NSArray *albums = [[self context] executeFetchRequest:fetchRequest error:&amp;error];
  </code></pre>
  
  <p>I think, I need to query like this, </p>
  
  <blockquote>
    <p>SELECT album.name, max(count(album.songs)) FROM Album;</p>
  </blockquote>
  
  <p>How to create such query in <code>NSFetchRequest</code>, is it possible with <code>NSExpression</code> or <code>NSPredicate</code>?</p>