In my api i have a JSON output like this;
{
"restaurants": [{
"Restaurant": {
"id": "3",
"name": "...",
"slug": "...",
"phone": "2161234567",
"address": "...",
"city_id": "34",
"county_id": "426",
"discount_type_id": null,
"discount": "",
"people_limit": "2",
"reservations_required": false,
"lat": "41.015608",
"lon": "28.934834",
"about": "...",
"logo": "...",
"logo_dir": "3",
"photo_1": "...",
"photo_2": null,
"photo_3": null,
"photo_dir": "3",
"created": "2012-11-30 16:30:21",
"modified": "2012-11-30 20:43:50",
"deleted": true
},
"AvailabilityRestriction": [{
"id": "1",
"title": "...",
"AvailabilityRestrictionRestaurant": {
"id": "28",
"restaurant_id": "3",
"availability_restriction_id": "1"
}
}],
"CuisineTag": [{
"id": "3",
"name": "...",
"CuisineTagRestaurant": {
"id": "47",
"restaurant_id": "3",
"cuisine_tag_id": "3"
}
}]
},
...
}
Restaurant has many cuisine_tags and availability restrictions. Also there’s a cities table… What’s the best way to sync them?
I have to sync cities, cuisine_tags and availability_restrictions first, then when creating restaurants i’ll match them? Is this the right way?
Thanks.
I would do it like this….
You should have all of the “Restaurant” properties (i.e. restirctions, cuisine-tags) within the restaurant object.
Here’s an example:
Also get rid of the “Restaurant” object, it’s unneeded. If I have an array called Restaurants. I want to only put/get Restaurants in/from it.
I think you had more to your question but I had trouble understanding what you need. Are you talking about how to set up relationships between them in Core Data?
If you are looking to set relationships between cruisine tags and cities or whatever I think this is the best way to go because you can loop throught the JSON response and if one of the attributes is a list you can determine the list and fetch the correct managed object and create the relationship.
Just to be clear, you should keep these cities, predefined cruise-tags in a different request though. You want to keep your API simple and flexible.