Rogare
Rogare

Reputation: 3274

How to have two NSManagedObject classes with the same superclass

I have two classes, Mammals and Fish, which both inherit from a general superclass, Animals. I'd like to store all my Mammals and Fish using Core Data, so I've made them both be subclasses of NSManagedObject. So, when I want to get all my Mammals and Fish, I just execute a couple fetch requests.

My question is what to do about the Animals superclass.

Sorry if this is a super-basic question, especially if the answer is just "Yes; No." One of the answers here warned against using inheritance in the data model, hence my apprehension.

Upvotes: 0

Views: 117

Answers (2)

Warren Burton
Warren Burton

Reputation: 17382

Make your own decision about whether your inheritance model will tie you up in knots in the future. But in direct answer to your question your inheritance tree will look like...

 NSManagedObject -> Animal -> Mammals 
                           -> Fish

so

@interface Animal : NSManagedObject

and

@interface Fish : Animal

Just be wary as Core-Data does not take well to complex rearrangement if you need to upgrade your model in the future and still hang on to existing data. Not impossible, just not always easy.

Upvotes: 1

Cy-4AH
Cy-4AH

Reputation: 4585

Make Animals subclass of NSManagedObject. Make Mammals and Fish subclass of Animals.

Upvotes: 1

Related Questions