Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save balazsnemeth/756fa0b6ca9823bcbff0241a66f0b1ca to your computer and use it in GitHub Desktop.
Save balazsnemeth/756fa0b6ca9823bcbff0241a66f0b1ca to your computer and use it in GitHub Desktop.
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