在 Core Data 中保存两个相互需要的多对多实体
我的 iOS 应用程序中有两个核心数据实体:目录和产品。它们都具有多对多关系,并且需要另一个实体。我用来填充这些实体的数据来自 XML 文件。目前,在 parser: didEndElement:
中,我保存了一个 Catalog 实体,如下所示:
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]);
}
}
}
但是,我不知道如何在不先保存 Product 来填充它的情况下保存 Catalog。有人有什么建议吗?如有必要,我可以发布更多代码来澄清这一点。
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 to 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以解析整个 xml 文件并在完成后保存上下文(我已经完成了,没有任何问题)。完成解析和创建所有实体后,您将链接所有产品和目录。这也将使您的文件解析速度更快。
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.