NSFetchRequest to-many key not allowed here
<p>I have a many to many relationship between two entities; <code>A</code> and <code>B</code>.</p>

<p>I want to return an array of <code>B.relationship</code> for every <code>A</code> where <code>B.relationship</code>’s count is greater than 0 and sorted by <code>B</code>’s <code>dateCreated</code> property.</p>

<p>This is the code I currently have which probably makes a little more sense.</p>

<pre><code>let fetchRecentVariationsRequest = NSFetchRequest(entityName: "Variation")
fetchRecentVariationsRequest.predicate = NSPredicate(format: "ANY activities.@count &gt; 0")
fetchRecentVariationsRequest.sortDescriptors = [NSSortDescriptor(key: "activities.dateCreated", ascending: true)]
</code></pre>

<p>When I run the request I get the following exception:</p>

<p><code>*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'to-many key not allowed here’</code></p>

<p>I understand why I’m getting the error but how I sort by a to many property for a sort descriptor in Core Data?</p>

<p><strong>Edit</strong></p>

<p>To be more clear, I’d like to fetch the 6 most recent <code>Activity</code> entities sorted by their <code>dateCreated</code> property (newest first).</p>

<p>Then I’d like to fetch all of the <code>Variation</code> entities which are related to these fetched <code>Activity</code> entities via the <code>Activity</code> entity’s <code>variations</code> relationship.</p>