title: NSFetchRequest with predicate/sort function closures
body: <p>I am rebuilding my app's CoreData stack and going to use a unit of work/repository pattern with generics.</p>
  
  <p>My set up is a <code>BaseRepository&lt;T:NSObject&gt;</code> generic class along with a <code>CoreDataRepository&lt;T:NSManagedObject&gt;</code> class that inherits from the base repository.</p>
  
  <p>I have four methods for retrieval: <code>getAll</code>, <code>getAllSorted</code>, <code>getAllFiltered</code>, and lastly <code>getAllFilteredSorted</code>.</p>
  
  <p>The <code>CoreDataRepository</code> will chain the first three <code>get</code> methods into the last one, passing in default values.</p>
  
  <p>Here is the definition of the designated method:</p>
  
  <pre><code>func getAllFilteredSorted(predicate:(T) -&gt; Bool, comparer:(T, T) -&gt; Bool) -&gt; T[]
      {
          let request = NSFetchRequest(entityName: entityName)
          let results = context.executeFetchRequest(request, error: nil)
  
          // TODO: Set predicate in Swift
          // TODO: Set sort descriptors in Swift
  
          return results as T[]
      }
  </code></pre>
  
  <p>However, I cannot find a way to create <code>NSPredicate</code> or <code>NSSortDescriptors</code> with these function closures. <code>NSManagedObjectContext.executeFetchRequest(...)</code> does not have an overload to take them either.</p>
  
  <p><strong>I know it is possible to sort/filter <em>after</em> the fact</strong>, but that reduces the optimization of my CoreData request. I'd prefer to do it beforehand.</p>
  
  <p>Any ideas on how to accomplish this? I don't mind if I can convert the closures to the proper objects, I just do not want to change my function's parameter definitions.</p>