• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: How to create subentity object inheriting from superentity object in core data
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: How to create subentity object inheriting from superentity object in core data


  • Subject: Re: How to create subentity object inheriting from superentity object in core data
  • From: Leon Starr <email@hidden>
  • Date: Wed, 16 Sep 2009 14:55:21 -0700

Okay, guys, this helps a lot, still stuck, but getting closer! Now I am getting an error with my revised code. We are talking about two entities A and B where B where A is the parent of B.
A has the property "name", and I would like to set that after creating an object for B.


Here is the code:

- (void)create_B: (NSString *)bName{
NSEntityDescription *bEntity = [[model entitiesByName] objectForKey:@"B"];


B *newB = [[OffDutyATC alloc] initWithEntity:bEntity insertIntoManagedObjectContext:context];
newB.name = bName; // << ERROR request for member 'name' in something not a structure or union
}


Now I have defined an NSManagedObject class for both A and B with the "name" property/dynamic set for A.name. But the B NSManagedObject class does not have any properties defined, so the error makes sense, but so much for normal inheritance!

So what is the correct approach here? Should I just use KVC to set the name property and not subclass NSManagedObject? Or should I add property/dynamic statements to my B subentity to mirror A? That seems kind of funky... Basically, what is the correct approach for setting the properties of a parent entity?

- Leon


From: Quincey Morris <email@hidden>
Date: September 16, 2009 11:22:46 AM PDT
To: cocoa-dev <email@hidden>
Subject: Re: How to create subentity object inheriting from superentity object in core data



On Sep 16, 2009, at 10:53, Leon Starr wrote:

I've got a generalization in my core data model with entities named A, B, C let's say where A is a super class with subentities B and C.

A is not abstract, so if I create an A NSManagedObject, I need to create and relate a single B or C subclass object. How do I make this happen? I can create the entities, but HOW do I tell the model that object B is a subclass of object A (or vice versa?)

Note: I did create the model programmatically and the subentities have been set properly for entity description "A".

Here's my sad attempt to move forward. As you can see, I've created the objects I need, but B doesn't know that A is it's superclass object. What to do?

NSEntityDescription *aEntity = [[model entitiesByName] objectForKey:@"A"];
NSEntityDescription *bEntity = [[model entitiesByName] objectForKey:@"B"];


A *newA = [[A alloc] initWithEntity:aEntity insertIntoManagedObjectContext:context];
B *newB = [[B alloc] initWithEntity:ATC_Entity insertIntoManagedObjectContext:context];
Now I have poured through all the related docs, and to my repeated consternation there are a million examples using stupid 1:1 or 1:M relationships but barely a whisper about programmatic entity inheritance. Arggh! Appreciate any help or pointers to whatever I missed in the docs.

I think a little terminology straightening might help you out.

Core Data doesn't have classes, it has entities. It doesn't have superclasses, it has parent entities. Entities and their parentage are defined in the Core Data model. If that's all you set up, all your Core Data objects will be of class NSManagedObject -- they'll all be the same Objective-C class, even though the entity parentage is correct and accounted for in Core Data's own data structures.

If you want you Core Data classes to have superclass ancestry to match their Core Data model parentage, you have to do it yourself. Define a custom NSManagedObject subclass corresponding to entity A, and define B and C subclasses of A (which are therefore also subclasses of NSManagedObject). In the entity descriptions for entities A, B and C, specify the corresponding class name.

Then, when you create a Core Data object the "normal" way (insertNewObjectForEntityForName:inManagedObjectContext:), Core Data will create the object with the correct Objective-C class, and you'll have the desired superclass ancestry.

Does that help?





_______________________________________________

Cocoa-dev mailing list (email@hidden)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden


  • Follow-Ups:
    • Re: How to create subentity object inheriting from superentity object in core data
      • From: Quincey Morris <email@hidden>
  • Prev by Date: Re: Custom Fonts in UI Webview
  • Next by Date: Re: NSUserDefaults synchronization in Foundation tools
  • Previous by thread: Re: Finding and NSView in a window
  • Next by thread: Re: How to create subentity object inheriting from superentity object in core data
  • Index(es):
    • Date
    • Thread