在 Objective C 中从 XML 中获取核心数据
我正在从 XML 中获取核心数据。但是,我想知道我不会插入相同的东西两次。
所以XML有表,例如
<Business>
<title>Sushi Tei</title>
<City>Jakarta</City>
</Business>
<Business>
<title>Sushi Fun</title>
<City>Jakarta</City>
</Business>
现在我不希望核心数据存储2个城市。核心数据必须认识到城市雅加达已经存在,请使用以前的城市记录。
问题是,当 NSXMLParser 到达开头时,我们不知道城市名称是否为雅加达。但我们通常使用它来创建新的 CoreDataObjects。
CoreData 是否具有“合并”功能,其中一条记录合并到较旧的记录中,然后合并记录的所有关系都会自动重新指向较旧的记录?或者什么是一个好的设计?
执行操作
One solution is to do
if ([elementName isEqualToString:@"Badger"])
{
self.currentBusiness=[NSEntityDescription insertNewObjectForEntityForName:@"Business" inManagedObjectContext:BNUtilitiesQuick.managedObjectContext];
}
if ([elementName isEqualToString:@"Building"])
{
self.currentBuilding=[NSEntityDescription insertNewObjectForEntityForName:@"Building" inManagedObjectContext:BNUtilitiesQuick.managedObjectContext];
self.currentBusiness.Building=self.currentBuilding;
}
if ([elementName isEqualToString:@"City"])
{
self.currentCity=[NSEntityDescription insertNewObjectForEntityForName:@"Building" inManagedObjectContext:BNUtilitiesQuick.managedObjectContext];
self.currentBusiness.City=self.currentCity;
}
一种解决方案是在 (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI QualifiedName:(NSString *)qName; 中
但那样就太复杂了。例如,我需要跟踪“商业”、“城市”的所有属性以及它们“某处”的所有子属性,然后重建整个对象。存储所有内容的最佳位置当然是核心数据。但在了解元素之前,我需要决定是否应该创建一个新的。我只需要一个经过验证的良好标准设计模式来使用 NSXMLParser 来核心数据。
我应该只使用 touchxml 吗?
另一种解决方案是将城市名称等数据放入 XML 的属性中,而不是作为子属性。这对我来说很好。这是唯一的方法吗? 唔....
I am grabbing Core Data from XML. However, I want to know that I am not inserting the same thing twice.
So the XML has table, for example
<Business>
<title>Sushi Tei</title>
<City>Jakarta</City>
</Business>
<Business>
<title>Sushi Fun</title>
<City>Jakarta</City>
</Business>
Now I do not want core data to store 2 cities. Core data must realize that city Jakarta already exist use the previous City record instead.
The problem is by the time NSXMLParser reach the opening, we do not know whether the city name will be Jakarta or not. But we usually create the new CoreDataObjects using that.
Does CoreData has a "merge" feature where one record got merged into an older record and then all the relationship of the merged record get automatically repointed to the older record? Or what would be a good design for this?
One solution is to do
One solution is to do
if ([elementName isEqualToString:@"Badger"])
{
self.currentBusiness=[NSEntityDescription insertNewObjectForEntityForName:@"Business" inManagedObjectContext:BNUtilitiesQuick.managedObjectContext];
}
if ([elementName isEqualToString:@"Building"])
{
self.currentBuilding=[NSEntityDescription insertNewObjectForEntityForName:@"Building" inManagedObjectContext:BNUtilitiesQuick.managedObjectContext];
self.currentBusiness.Building=self.currentBuilding;
}
if ([elementName isEqualToString:@"City"])
{
self.currentCity=[NSEntityDescription insertNewObjectForEntityForName:@"Building" inManagedObjectContext:BNUtilitiesQuick.managedObjectContext];
self.currentBusiness.City=self.currentCity;
}
in (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName;
But that would be too complicated. For example, I need to keep track of all the attributes of Business, Cities, along with all their child attribute "somewhere" and then reconstruct the whole object after that. The best place to store all that is of course in the core data. But I need to decide whether I should create a new one or not before I know the elements. I just need a good proven standard design pattern for using NSXMLParser to core data.
Should I just use touchxml?
Another solution is to put data like name of the city in the attributes of the XML rather than as a child. That's fine for me. Is that the only way?
Hmm....
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我相信您必须在创建记录之前手动检查(使用 NSFetchRequest)记录是否存在。如果存在,则不创建新记录,但如果不存在,则创建该记录。
I believe that you will have to manually check (using NSFetchRequest) if a record exists before creating it. If it does exist, don't create a new record, but if it doesn't exist then create the record.
如果城市记录是单独的实体,而不仅仅是业务实体的属性,则您可以完成此任务。我不知道任何内置的核心数据功能可以为您强制执行唯一性,因此您必须自己构建它。实现此任务的最简单方法是遵循索斯伯恩在另一个答案中的建议。从 XML 中读取城市名称,并使用 NSFetchRequest 在数据中查找该城市。如果找到城市,请将找到的城市实体插入到您的业务实体中。如果您找不到城市,请创建一个新的城市实体以插入到您的业务实体中。
If City records are separate entities, not merely attributes of the Business entity, you can accomplish this task. I'm not aware of any built in Core Data functionality that would enforce uniqueness for you, so you'll have to build that your self. The easiest way to implement this task is to follow sosborn's advice in another answer. Read the city name from your XML and use an NSFetchRequest to find that city in your data. If you find the city, insert the found City entity into your Business entity. If you don't find the city, create a new City entity to insert into your Business entity.