Lets assume we have an entity called “Document” and an other one called “User”. There is a many-to-many relationship so a user can have a set of documents and each document can be owned by multiple users. Further documents have the attribute “type”.
Now I want to fetch all documents for user x with the type “pdf”. How would you access this data?
As far as I can see there are two ways:
-
Retrieve all documents from the user by its relationship (user.documents) and filter the array with a NSPredicate (does that work with a complex object?).
-
Do a fetch request on the “Documents” entity with two predicates. The first one specifies the user and the second one is responsible for the type.
Which is the better approach? Or is there even a better way to achieve this?
I guess the second solution is much more expensive.
Your request’s entity should be Document, and you need to use the ANY operation in your predicate:
See the section on Aggregate Operations:
http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/Predicates/Articles/pSyntax.html