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....