CoreData Abstract entity with relationship

2019-08-10 01:52发布

问题:

I have this situation:

I have two CoreData entities:

MUSIC_ACTIVITY is an ABSTRACT CoreData entity;

LOCATION: with some attributes as: city, address,...

BAR: with some attributes; PUB: with some attributes;

in MUSIC_ACTIVITY I have a relationship with LOCATION (1to1).

MUSIC_ACTIVITY is the PARENT entity of BAR; MUSIC_ACTIVITY is the PARENT entity of PUB;

I generate automatically class of this model.. So: In MUSIC_ACTIVITY I have one property LOCATION In LOCATION I have property MUSIC_ACTIVITY (inverse)

How can I access to LOCATION information into BAR and PUB entity ?

for example: I declarated the relationship with LOCATION using 'address' (like name). So in MUSIC_ACTIVITY I have a property address (LOCATION type). In PUB I need to overwrite the init..for example:

-(id) initWithEntity:(NSEntityDescription *)entity insertIntoManagedObjectContext:(NSManagedObjectContext *)context
{
    self = [super initWithEntity:entity insertIntoManagedObjectContext:context];
    if (self) {
        self.name = @"MY NAME";
        self.address = .... ///ERROR ////
    }
    return self;
}

but I can't use self.address ...

回答1:

You can access parent entity's attributes the same as you would the properties of a parent class.

Just some debugging tips for entity inheritance in XCode:

  • Double check the parent entity
  • Change the outline style of the xcdatamodel viewer to hierarchical

I also recommend MOGenerator. You don't have to manually recreate the backing class definitions when you change the model.