I have two core data entities in my iOS app, Catalog and Product. They both possess a many-to-many relationship with require to the other entity. The data that I'm using t开发者_Go百科o populate these entities are from an XML file. Currently, in parser: didEndElement:
I save a Catalog entity, like so:
if ([elementName isEqualToString:@"catalog"])
{
// Sanity check
if(currentEatery != nil)
{
NSError *error;
// Store what we imported already
if (![managedObjectContext save:&error]) {
// Handle the error.
NSLog(@"Eatery error: %@", [error domain]);
}
}
}
However, I do not know how to save Catalog without first saving a Product to populate it with. Does anyone have any suggestions? I can post more code to clarify this if necessary.
You can parse your entire xml file and save the context when you are done (I've done it with no problems). Once you have finished parsing and creating all entities you will have all the products and catalogs linked. This will also make your file parsing faster.
精彩评论