jeudi 5 juin 2014

NSFetchRequest with predicate/sort function closures


Vote count:

0




I am rebuilding my app's CoreData stack and going to use a unit of work/repository pattern with generics.


My set up is a BaseRepository<T:NSObject> generic class along with a CoreDataRepository<T:NSManagedObject> class that inherits from the base repository.


I have four methods for retrieval: getAll, getAllSorted, getAllFiltered, and lastly getAllFilteredSorted.


The CoreDataRepository will chain the first three get methods into the last one, passing in default values.


Here is the definition of the designated method:



func getAllFilteredSorted(predicate:(T) -> Bool, comparer:(T, T) -> Bool) -> 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[]
}


However, I cannot find a way to create NSPredicate or NSSortDescriptors with these function closures. NSManagedObjectContext.executeFetchRequest(...) does not have an overload to take them either.


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.



asked 19 secs ago

SiLo

1,774





Aucun commentaire:

Enregistrer un commentaire