theDuncs
theDuncs

Reputation: 4843

Get the Type of a Core Data relationship

Is there an easy way to find the object type which forms a specific relationship in Core Data?

For example, I have a one-to-many relationship:

Battery-----1-to-Many-----Comment

If I didn't know that the relationship was for a specific Comment object, is there a programatic way I could find out which object type it is, based solely on the set that I'm dealing with.

Something along the lines of

battery.comments.classType = [Comment class]

I'm aware that both Battery and Comment are of type NSManagedObject - I'd like to know more specifically what they are.

I'm also aware that if the NSSet contains any data, I can use any one of it's objects to query the type. However I need to cater for when there is no data in the NSSet. Thank you.

Upvotes: 0

Views: 443

Answers (2)

Mark Kryzhanouski
Mark Kryzhanouski

Reputation: 7241

You can get all info you need from this few lines:

    NSRelationshipDescription* rel = [[[battery entity] relationshipsByName] valueForKey:@"comments"];
    NSString* className = [[rel destinationEntity] managedObjectClassName];
    NSString* entityName = [[rel destinationEntity] name];

Upvotes: 3

Levi
Levi

Reputation: 7343

In your AppDelegate you have an NSManagedObjectModel typed property. It has an entities array containing NSEntityDescriptions. From here you should be able to figure it out. Hope this helps!

Upvotes: 0

Related Questions