Cocoa - 从XML加载数据期间应用程序冻结

时间:2012-05-15 15:44:22

标签: objective-c cocoa nsprogressindicator

我正在从XML文件中加载大量数据。这些数据“冻结”了应用程序。我想在加载数据的时候显示一个进度条。

我怎么知道大部分操作已经完成,以便我可以更改进度条显示的百分比?谢谢。 [回答]

编辑:主要问题是由于要解析的大量数据,应用程序在加载期间冻结。有人建议使用后台线程来加载和解析数据。这可行吗?

2 个答案:

答案 0 :(得分:2)

一次将文件读入内存@ 4k?显示加载字节与文件大小的过程?

另外计算出总行数以及解析器所在的当前行。

https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSXMLParser_Class/Reference/Reference.html#//apple_ref/occ/instm/NSXMLParser/lineNumber

假设您使用的是NSXMLParser

“冻结”来自你在主线程上进行加载/解析。如果你能让我们GCD尝试执行dispatch_async()并在完成时向主线程发送更新并更新进度。

答案 1 :(得分:0)

如果您使用的是NSXML解析器,

NSUInteger count = [[rootElement nodesForXPath:@"//Video" error:&error] count];
//this will give you the number of elements
//then after parsing each element, calculate the percentage of elements parsed. 

//i.e. num_of_elements_parsed/total_no_elements*100;
//based on this value, use a del;egate method to update your NSProgressIndicator 
// in this method increment the  num_of_elements_parsed counter
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict {

    if ([elementName isEqualToString:@"resident"]) {

        self.count += 1; 

    }
}
相关问题