0
votes

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

2
A word of advice: Core Data is not SQL. Entities are not tables. Objects are not rows. Attributes are not columns. Relationships are not joins. Core Data is an object graph management system that may or may not persist the object graph and may or may not use SQL far behind the scenes to do so. Trying to think of Core Data in SQL terms will cause you to completely misunderstand Core Data and result in much grief and wasted time.TechZen
TechZen hit it on the head. CoreData is great for this stuff but you have to understand how it differs from a database.sosborn

2 Answers

1
votes

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.

0
votes

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.