在我的应用程序中,我需要将100MB xml文件导入到核心数据模型中.
到目前为止,我已经导入了一个100KB的xml文件,一切正常.但是,不确定导入100MB xml需要多长时间.我会在一段时间内运行它.你觉得这种做法好吗?
谢谢
NSManagedObjectContext * context = [self managedObjectContext]; // Delete all documents 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]; //Insert documents 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
这是一次性操作,仅在模拟器中运行,并且不会与最终应用程序一起部署.