31
votes

Suppose I have a Core Data model with an abstract entity called "Animal." Then, I have many other entities that inherit from this abstract entity: "Lion", "Dog", "Cat", etc. (I'm not developing a zoo program, but this analogy works well for the issue I'm explaining!)

What I want to know is: Can I fetch "all animals" at once by doing this:

NSFetchRequest *searchRequest = [[NSFetchRequest alloc] init];
[searchRequest setEntity:[NSEntityDescription entityForName:@"Animal" inManagedObjectContext:aContext]];

NSArray *matchedObjects = [aContext executeFetchRequest:searchRequest error:nil];

I understand there are methods on NSEntityDescription to determine whether an entity inherits from another. But is there a fast way to grab all entities that are of a particular (abstract) type --- in this case, "Animal"?

If the approach above is invalid, what is the most efficient way to go about this? Thanks!

3

3 Answers

32
votes

You can definitely use that approach.

From Apple's Core Data Programming guide:

Entity inheritance works in a similar way to class inheritance; and is useful for the same reasons. If you have a number of entities that are similar, you can factor the common properties into a superentity, also known as a parent entity. Rather than specifying the same properties in several entities, you can define them in one entity, and the subentities inherit them. For example, you might define a Person entity with attributes firstName and lastName, and subentities Employee and Customer, which inherit those attributes.

2
votes

I've done something similar, however that entity was not abstract. It was a standard entity (with no instances) and the other entities that I fetched were all derived from that entity. I haven't tried it with an abstract class, however, looking at the docs it appears that it might not be possible:

Core Data Programming Guide

Abstract Entities You can specify that an entity is abstract—that is, that you will not create any instances of that entity. You typically make an entity abstract if you have a number of entities that all represent specializations of (inherit from) a common entity which should not itself be instantiated. For example, in a drawing application you might have a Graphic entity that defines attributes for x and y coordinates, color, and drawing bounds. You never, though, instantiate a Graphic. Concrete sub-entities of Graphic might be Circle, TextArea, and Line.

My suggestion would be to set things up with the abstract Animal entity and give it a shot. If it doesn't work then just make the Animal entity non-abstract (words are failing me, is that what we would call it? Perhaps 'concrete' is better?) and you should be fine. If anyone has done this with an abstract class I would to hear about it.

0
votes

Yes, I believe you can fetch all the Animals at once.

From the Core Data Programming guide:

Entity Inheritance

If you specify a parent entity in Core Data as the entity for a Core Data fetch request, the fetch returns matching instances of the entity and subentities (see Fetching Objects). As a corollary, if you specify a parent entity as the entity for a controller that is backed by Core Data, it fetches matching instances of the entity and any subentities. If you specify an abstract parent entity, the Core Data backed controller fetches matching instances of concrete subentities.