Q: Save dynamically generated JSON data with parent-->children relationship in core data? I have dynamic child objects in my JSON response to populate preferences data, each preference can have "children" and that can have multiple objects (in nested way, go into details). I need to save this data into Core data then load from there. Also I need to save user selection when user selects one on the way. 


*

*what is the best way to implement entity relationship in entity model to save all this data ?

*Is this a good JSON format for this kind of scenario?
EX: Mobiles --> samsung --> s3 ( this can go further when user clicks)
    preferences =     [
        {       
          id = 1;
          name = Mobiles;
          children = 
            [
                { 
                    id = 3;
                    name = Samsung;
                    children = [];
                },
                { 
                    id = 4;
                    name = Nokia;
                    children = [];
                }
           ] 
        },
        {
            id = 2;
            name = Electronics;
            children =  [
                { 
                    id = 5;
                    name = Samsung Curve TV;
                    children = [];
                }
            ]
  }
    ]


A: ad 2. Using this data format, you will need to use recursion or stack to get single entity.
I suggest a slightly different approach.
[
  {
    "id": 1,
    "name": "Mobiles"
  },
  {
    "id": 2,
    "name": "Electronics",
    "parent_id": 1
  },
  {
    "id": 3,
    "name": "Samsung"
    "parent_id": 1
  },
  {
    "id": 4,
    "name": "Nokia"
  },
  {
    "id": 5,
    "name": "Samsung Curve TV"
    "parent_id": 2
  }
]

With such structure you won't have to use recursion or stack.
A single enumeration is enough to insert data into CoreData.
ad 1. Your entities need to have "parent" and "children" relationships.
