开发者

Grabbing Core Data from XML in Objective C

开发者 https://www.devze.com 2023-03-03 15:57 出处:网络
I am grabbing Core Data from XML. However, I want to know that I am not inserting the same thing twice.

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开发者_JAVA百科, 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....


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号