Given the following contrived example:

I would like to request my details for all objects that are either cats or dogs. I want the result set to be ordered by name, regardless of type, so fetching all cats that pull all dogs out will not. I want to do this in one request.
One way to do this is to add the PetType field to Pet, give each record a petType value that identifies the subsystem to which it belongs, then run the query as follows:
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Pet" inManagedObjectContext:myMOC]; [fetchRequest setEntity:entity];
But the very thought of how this is done makes me shudder. Is there a better way?
Update : thanks to everyone who answered - there are really good, well-thought-out solutions here, and I appreciate them all.
To give this context, the actual data model is a little more complicated than this (not always), but it is pretty well organized. At one time, I developed more than my fair share of data schemas, and I'm glad that entities and their relationships are well considered. This problem arose because (to extend an already shaky far-fetched example) that the client originally wanted:
- view showing a list of all pets.
- view showing a list of goldfish
- view showing a list of cats
- view showing a list of dogs
So far so good. But they also want to see a general list of all cats and dogs "because little girls love cats and dogs." (For the same reason, at first it was cats and goldfish.) There really is no way to naturally group this subset of specific objects; it is really quite arbitrary.
Until now, Dave Dribin's "abstract intermediate entity" approach seems to be the purest solution, although in my case I think it will be somewhat artificial; in fact the only way you could honestly label an intermediate object would be "ThingLittleGirlsLike"! :)
objective-c cocoa core-data
Simon whitaker
source share