0

I have a question to ask regarding Coredata entity relationships. Can one entity have multiple relations with another entity. I have the following json:

{
"user": {
    "user_id": "192837190283",
    "email": "ceo@me.com",
    "first_name": "Tim",
    "last_name": "Cook",
    "home_address": {
        "street": "Downing Street",
        "number": "11",
        "city": "London",
        "state": "Greater London",
        "country": "United Kingdom",
        "zip": "12323423",
        "location": {
            "latitude": 3.1,
            "longitude": 3.2
        }
    },
    "work_address": {
        "street": "Amphitheatre Parkway",
        "number": "1600",
        "city": "Mountain View",
        "state": "California",
        "country": "United States",
        "zip": "94043",
        "location": {
            "latitude": 3.1,
            "longitude": 3.2
        }
    }
}
}

I have the follow model in Coredata :

enter image description here Still, when I parse json, it still saves only one address. Am I doing something wrong with relations? Please suggest. Thanks in advance.

Lorenzo B
  • 33,216
  • 24
  • 116
  • 190
Anand
  • 864
  • 10
  • 31

1 Answers1

0

This is a very strange model. First, read my previous answer on how to creating a parent-child relationship in Core Data.

Setting up a parent-child relationship in Core Data

Then, about your model, I would modify a little bit. In particular, I would have a User, an Address and a Location as you did.

A user will have one or more address by means of a relationship called addresses. An address will have a location through a one-to-one relationship called location.

The most important thing, it is to remove homeUser and workUser relationships and add a boolean attribute (for example isWorkAddress) that let you specify if the address is the home or the work one. This attribute will be added to Address entity. When you parse the JSON, you will set the attribute in the correct way.

Does it work for you?

Community
  • 1
  • 1
Lorenzo B
  • 33,216
  • 24
  • 116
  • 190
  • well, in the project I am working, there are places where json has array object, In those places I have used to many relationships but in the json I shared, address is not as an array but separate entities. So, I was not sure (though it was in my mind) to create to-many relationship from user to address. My problem is that I am not parsing json manually but it is being done automatically (I modified JAGPropertyConverter lib to support NSManagedObject) and in that lib while NSDictionary being converted to NSManagedObject, it must match the exact attributes as in json. – Anand Nov 21 '13 at 09:54
  • and the isWorkAddress or isHomeAddress is not found in json and I cannot set it separately. – Anand Nov 21 '13 at 09:55
  • @Joy But that attribute MUST be set on model. Obviously you cannot find it in your JSON. Yuo parse the JSON, verify if the JSON element is a work address or home one, insert a new entity with a true or false value based on that parsing. – Lorenzo B Nov 21 '13 at 10:00
  • I guess, there is no other choice, So, I will try like you said. But if there would have been an extra field in json "address_type" and put all the addresses in array, that would have been much better. – Anand Nov 22 '13 at 08:31