In my application, I need to import a 100 MB file into the underlying data model.
So far, I have imported a 100K xml file and everything is working fine. However, I’m not sure how long it will take to import 100 MB xml. I will launch it with tonite, while ... do you think this approach is good?
thank
NSManagedObjectContext * context = [self managedObjectContext];
NSFetchRequest * fetch = [[[NSFetchRequest alloc] init] autorelease];
[fetch setEntity:[NSEntityDescription entityForName:@"Document" inManagedObjectContext:context]];
NSArray * result = [context executeFetchRequest:fetch error:nil];
for (id basket in result)
[context deleteObject:basket];
TBXML * tbxml = [[TBXML tbxmlWithXMLFile:@"categ_small.xml"] retain];
TBXMLElement * root = tbxml.rootXMLElement;
TBXMLElement * doc = [TBXML childElementNamed:@"doc" parentElement:root];
do {
TBXMLElement * idDoc = [TBXML childElementNamed:@"id" parentElement:doc];
TBXMLElement * titleDoc = [TBXML childElementNamed:@"title" parentElement:doc];
TBXMLElement * descriptionDoc = [TBXML childElementNamed:@"description" parentElement:doc];
TBXMLElement * time = [TBXML childElementNamed:@"time" parentElement:doc];
TBXMLElement * tags = [TBXML childElementNamed:@"tags" parentElement:doc];
TBXMLElement * geo = [TBXML childElementNamed:@"geo" parentElement:doc];
TBXMLElement * event = [TBXML childElementNamed:@"event" parentElement:doc];
TBXMLElement * user = [TBXML childElementNamed:@"user" parentElement:doc];
TBXMLElement * categ = [TBXML childElementNamed:@"categ" parentElement:doc];
NSManagedObject *newDocument = [NSEntityDescription
insertNewObjectForEntityForName:@"Document"
inManagedObjectContext:context];
[newDocument setValue:[TBXML textForElement:idDoc] forKey:@"idDoc"];
[newDocument setValue:[TBXML textForElement:titleDoc] forKey:@"titleDoc"];
[newDocument setValue:[TBXML textForElement:descriptionDoc] forKey:@"descriptionDoc"];
[newDocument setValue:[TBXML textForElement:time] forKey:@"time"];
[newDocument setValue:[TBXML textForElement:tags] forKey:@"tags"];
[newDocument setValue:[TBXML textForElement:geo] forKey:@"geo"];
[newDocument setValue:[TBXML textForElement:event] forKey:@"event"];
[newDocument setValue:[TBXML textForElement:user] forKey:@"user"];
[newDocument setValue:[TBXML textForElement:categ] forKey:@"categ"];
} while ((doc = doc->nextSibling));
UPDATE
This is a one-time operation that is performed only in the simulator and it will not be deployed with the final application.
source
share