Search code examples
iosiphonedatabasecore-dataiphonecoredatarecipes

Core Data - Relations queries


In core data,

I want to use relationship. But I have a doubt in my mind. Apologies but I didn't find clear answers on various websites and blogs. Other things like Add, Fetch, Delete queries are clear to me. But I have some questions in core data which are still not clear.

I have made two Entities:

  • Photographer [Attributes : name and camera]
  • Photo [Attributes: zoner and photographerName] .

1) I have connected them with relations. So if I connect this two entities with relations then I should remove that photographerName from Photo?

2) As I have connected these two entities with the relation then how can I use the photographer name with the photo Entity?

3) How can I add values in this if I use it with relations? [Now it is showing me Null in the relation from sqlite browser]


Solution

  • So if I connect this two entities with relations then I should remove that PhotographerName from Photo ?

    Generally, yes, it is redundant.

    how can I use the photographer name with the photo Entity ?

    self.photographer.name
    

    (assuming you are in the Photo class and the relationship names is photographer)

    How can I add values in this if I use it with relations

    I guess you mean how can I set the relationship value. Create an instance (or find an existing instance) or each entity and then:

    photo.photographer = photographer;
    

    Notes:

    1. Ensure that the relationship has an inverse
    2. Names the relationship ends photographer and photos (1 to many)
    3. Try to set the photographer of a photo, or use the relationship methods auto-generated in the Photographer class to add photos