0

I am setting up Core Data in my app programmatically, and have come across an "issue" (not exactly an issue, but was wondering if anyone had found a nice way around it).

I have a base class FormElement (which extends NSManagedObject). I then extend this for various form types, e.g. TextFieldFormElement, TextAreaFormElement and SwitchFormElement, all extend FormElement. I also have a FileAsset class (which extends NSManagedObject) that stores information about files. TextFieldFormElement and TextAreaFormElement have a to-many relationship with FileAsset but SwitchFormElement does not.

I understand that I don't HAVE to sent an inverse relationship for FileAsset, or that I could set multiple properties in FileAsset as the inverse for each FormElement that can have a relationship with FileAsset (e.g. FileAsset.textFieldElement, FileAsset.textAreaElement etc).

My question is: has anyone found a nice workable solution for allowing an inverse relationship with any managed object of a certain type? What I really want to do is simply have one property in FileAsset (e.g. FileAsset.element) that will take any object of type FormElement (and so, any object that extends FormElement)?

Programmatically I can't see an obvious was to achieve this:
TextFieldFormElement >(one-to-many)> FileAsset
TextAreaFormElement >(one-to-many)> FileAsset
FileAsset >(one-to-one)> FormElement

Has anyone ran in to a problem like this before and found a nice solution? I guess my other option would be to ensure FormElement simply encompasses all possible properties for a form element and do-away with extending FormElement.

Many thanks!

Vinay Bagale
  • 2,361
  • 4
  • 36
  • 45

1 Answers1

0

I would define assets as a relationship from the base class FormElement to FileAsset and make it optional. Then element is the inverse relationship from FileAsset to FormElement, and can be set to an element of the base class or any derived class.

Martin R
  • 529,903
  • 94
  • 1,240
  • 1,382