1

i have a simple iphone project which contains a simple xcdatamodel that has a single entity with roughly 3 attributes..

i want to know if there is a way to programmatically add an attribute to an entity.. i.e. if the user presses an "add" button of some kind, a simple string attribute is added to the entity and saved..

If this is not possible could someone point me in the right direction..

vikingosegundo
  • 52,040
  • 14
  • 137
  • 178
Ibz
  • 518
  • 1
  • 8
  • 26

2 Answers2

5

You can programmatically alter entities but you can't alter a managed object model after it has been assigned to a managed object context so that makes it useless for any user defined alterations. In any case, you wouldn't want to add entities programmatically because that would make your previously created persistent store file useless.

If you want to create a more free-form , user extensible data model, you have to back out and make your entities more flexible by adding an optional relationship to another entity or entity inheritance group that can model additional data.

For example: Suppose you have a contact list and you want to add free form fields to each contact. You would set up your entities like this.

Contact{
    name:string
    phone:string
    userDefinedFields<-->>UserDefined.contact
}

UserDefined{
    name:string
    contact<<-->Contact.userDefinedFields
}

Whenever the user adds a new field, you create a new UserDefined object and add it the Contact.userDefinedFeilds relationship. You can flesh that out as needed. If you need more than one type of user defined field you should set it up like this:

Contact{
    name:string
    phone:string
    userDefinedFields<-->>UserDefined.contact
}

UserDefined{
    name:string
    contact<<-->Contact.userDefinedFields
}

TextField:UserDefined{
    text:string
}

NumberField:UserDefined{
    numValue:Number
}

You can then drop in a TextField or NumberField object into the Contact.userDefinedFields as needed.

TechZen
  • 64,370
  • 15
  • 118
  • 145
  • That is exactly what i wanted to do!! I really appreciate the time you've taken to explain it! Do you know if i might find a tutorial/sample code somewhere, as i am still learning development for iphone and am not sure how i would go about 'fleshing' it out.. In either case i appreciate your response :) – Ibz Apr 04 '11 at 22:42
  • I don't know of any tutorials for expressly this type of data model. By "flesh out" I mean you can add any additional attributes or subentities might be required for your particular needs. – TechZen Apr 05 '11 at 18:50
  • Downvoted for the comment: "In any case, you wouldn't want to add entities programmatically because that would make your previously created persistent store file useless." Generally, if you're doing these kinds of alterations at runtime, you can, but you'll need to perform a migration. This means copying your model, modifying it, creating an inferred mapping model from your current and modified model and performing the migration of your old persistent store to a new one. It's more work, but it's certainly possible. – Luke Redpath Sep 04 '12 at 12:29
1

i am not so sure if you can add an attribute with code, but maybe you can consider using one to many relationship?

Rae
  • 131
  • 1
  • 6
  • so if i have a one to many relationship with 2 entities.. Entity1 and Entity2.. what kind of code would i write to create if i want to have 3 of Entity2 linked to one Entity1.. – Ibz Apr 04 '11 at 11:56