Core Data import over HTTP with to-Many relation
I am new to core data and I have a few questions. We are developing an app that pretty much serves the function of a business directory. Therefore I have 2 entities. Category and Businesses. Any busines开发者_JAVA技巧s can have 1 or more categories. Any category can have a parent category.
Now the problem is that we are getting our information from a web service so when the app runs for the first time it fetches information from the server.
Now, I am thinking about the most efficient way to import this data into core data. At first I thought of creating 2 dictionaries 1 with categories, and another one with businesses, then import categories, second import businesses, but then I think I will have to go back and set the relations between the two.
Then I thought about just creating one dictionary with categories and a dictionary of businesses inside of it but that would mean that we will be receiving duplicated business information.
What I want to do is to take the information from the server and be able to import it to core date with its relations and without having to loop too many times within these 2 sets of information. So any pointers on how to do this will be welcome.
PS. We are also creating the web service so the information can come in any way we want.
Thanks for your help.
I dont know where the exact problem is. The only thing to do is to create the coredata-model with two entities: Category and Business which are linked with a 1-to-many-relationship (you should also set the reverse-direction)
I would implement the sync-mechanism via a Restful-Service but you dont have to. Just write a script on the server which delivers your needed data in xml or json. The iPhone is then able to parse that data and inserts the data to coredata.
an xml-exmaple
<xml>
<business name="alpha">
<category name="cat1" />
<category name="cat2" />
</business>
<business name="beta">
....
</business>
...
</xml>
edit:
so just create the elements at parsing xml (this is just the principle):
XmlElement* rootElement = [MyParser parseXmlAndGetDomRootElement:xmlString];
for(XmlElement* businessElement in [rootElement getAllChildrenWithName:@"business"])
{
Business b = [MyCoreDataHelper createBusiness];
b.name = [businessElement attributeForName: @"name"];
for(XmlElement* categoryElement in [b getAllChildrenWithName:@"category"])
{
Category c = [MyCoreDataHelper createCategory];
c.name = [c attributeForName:@"name"];
c.business = b; //this will also insert the category to the categories-list of b if you set up your model correctly
}
}
[MyCoreDataHelper saveChanges];
so no dict is needed
精彩评论