0

I have a rest webservice that gets me the a json response in the following structure :

{
"Categories": [
    {
        "category_id":1,
        "category_name":"category 1"
    },
     {
        "category_id":2,
        "category_name":"category 2"
    }
],
"Products":[
    {
        "product_id":1,
        "product_name":"Product 1",
        "category_id":1
    },
    {
        "product_id":2,
        "product_name":"Product 2",
        "category_id":1
    },
    {
        "product_id":3,
        "product_name":"Product 3",
        "category_id":2
    }

]    
}

I am creating a Core data model for my iOS application in which i can save this data as you can see below .

Core data model

The problem is how can I use the relationships that I have created in my core data model to map this data?

The silliest idea that comes to my mind is to search for each product's category based on the category id and then set it like Product.category = category [i] , but there got to be a easier way to do this.

Tapan Thaker
  • 362
  • 5
  • 13
  • You might want to look at using RestKit. – Wain Oct 16 '13 at 11:23
  • Yes , got something of a similar interest on RestKit here on [SO](http://stackoverflow.com/questions/14854641/restkit-and-saving-to-coredata-as-nsmanagedobject). But I really have coded a lot already using [ObjectMapper](https://github.com/MuhammadHewedy/ObjectMapper). – Tapan Thaker Oct 16 '13 at 11:49

1 Answers1

0

Based on your use of ObjectMapper you will need to make the connections yourself using your 'silly idea (TM)'. You should look at doing the fetch as a batch and sorting the results to minimise calls to Core Data and to make the mapping simple.

If you chose to use RestKit then you could just configure the mappings for the objects and the identities used to link them and RestKit will do the lifting work. It could be a bit of work for you...

Wain
  • 118,658
  • 15
  • 128
  • 151