Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Trick, how to add a generic function available for each NSManagedObject subclass
extension NSObjectProtocol where Self: NSManagedObject {
static func findAllWithPredicate(searchTerm: NSPredicate?, inContext context: NSManagedObjectContext = NSManagedObjectContext.MR_defaultContext()) -> [Self] {
//You can implement the fetchrequest here by yourself, but now I'm using MagicalRecord service to keep it simple
return self.MR_findAllWithPredicate(searchTerm, inContext: context) as! [Self]
}
}
Usage (There is no cast required here, but the result is a [AnswerObject]):
let unsynchedObjects = AnswerObject.findAllWithPredicate(NSPredicate(format: "synchronized == false"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.