title: Custom query methods in NSManagedObejct
body: <p>Is there a way to add a custom query method in a subclass of NSManagedObject?</p>
  
  <p>I have a domain model called User, and a domain model called Message. I've set it up so that a user has a To-Many relationship to messages. So in my app, when i do </p>
  
  <pre><code>[userInstance messages];
  </code></pre>
  
  <p>it returns me a NSSet of messages that belong to that user, which is perfect. But the Message domain model also has an "active" field which is a boolean.</p>
  
  <p>I want to create a new method on the domain model that says</p>
  
  <pre><code>- (NSSet *)activeMessages
  </code></pre>
  
  <p>which, obviously, is a method that only return messages with active = true. I know I could do</p>
  
  <pre><code>[userInstance messages];
  </code></pre>
  
  <p>then just loop through and remove all the ones which are active = false, but that seems a bit inefficient. Seems like a waste to fetch all, then filter. I would rather just filter it out in the query.</p>
  
  <p>The only other way I can think of, is to somehow get a hold of the NSManagedObjectContext inside the domain model to manually do the query.</p>
  
  <p>Is that the best way? or is there something else I can do?</p>