1
votes

In my app, Im using CoreData and I have two entities. VenueInfo and ContactInfo.

In the app, you can add venues, and each venue has ContactInfo. So I've setup a one to one relationship in my model.

So I would imagine, I could simply do the following:

[venue.contact setValue:textField.text forKey:email];

So like so you'd set the email attribute of the contact object which belongs to the venue. However this doesn't work.

Is it possible because the contact object doesn't exist yet?

1
Please post a screenshot of your .xcodeModel...rohan-patel
I updated my answer. Please have a look..rohan-patel

1 Answers

4
votes

The line you wrote won't work that way. I assume contact is the relationship name you have in your Venue entity in xCodeModel.

 [venue.contact setValue:textField.text forKey:email]; // can't write this way..

Suppose your ContactInfo entity has two fields : phone, email so this way you can go. and VenueInfo entity has two fields : name.

  VenueInfo *venueInfo=[NSEntityDescription   insertNewObjectForEntityForName:@"VenueInfo" inManagedObjectContext:self.managedObjectContext];
  vanueInfo.name=txtVenueName.text;           // venue name entry


  ContactInfo *contactInfo=[NSEntityDescription   insertNewObjectForEntityForName:@"ContactInfo" inManagedObjectContext:self.managedObjectContext];

  contactInfo.phone=txtPhone.text;
  contactInfo.email=txtEmail.text;
  contactInfo.venue=venueInfo;  // I assume venue is the relationship name you give in ContactInfo entity towards Venue entity.

What we did here is we saved phone and email details to ContactInfo entity only and then we just related it with currently selected VenueInfo accessing the relationship we declared in ContactInfo Entity for VenueInfo.

This maybe quite confusing so I'll recommend you to follow a few tutorials which will give you an idea to grasp some basics about core data relationships.