我正在开发我的第一个应用程序,它从网络上获取XML提要。
第一次将XML加载到TableView
时,一切正常,但是当我想通过点击按钮重新加载数据时,它会在重新加载XML和表视图之前停止几秒钟。我怎样才能避免这种停顿时间?
当我点击表格单元格转到下一个TableViewController
时,它会在加载XML Feed以显示列表时暂停几秒钟。
我的应用程序就像iPhone的原生博客应用程序,并且有类别和子类别。
答案 0 :(得分:0)
参考以下过程。
@interface YourViewController : UITableViewController<NSURLConnectionDelegate,NSXMLParserDelegate>
//some declartion property...
@end
@implementation YourViewController
- (void)viewDidLoad
{
[super viewDidLoad];
isParsingStart = NO;
webData = [[NSMutableData alloc] init];
connection = [[NSURLConnection alloc] initWithRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:YOUR_XML_URL] cachePolicy:NSURLRequestReturnCacheDataElseLoad timeoutInterval:20] delegate:self];
}
- (void)tappingButton:(UIButton *)sender
{
if(isParsingStart)
{
//show a alert view... or other do stuff
return;
}
webData = [[NSMutableData alloc] init];
connection = [[[NSURLConnection alloc] initWithRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:YOUR_XML_URL] cachePolicy:NSURLRequestReturnCacheDataElseLoad timeoutInterval:20] delegate:self] autorelease];
}
- (void) connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
[webData appendData:data];
}
- (void) connectionDidFinishLoading:(NSURLConnection *)connection
{
parser = [[NSXMLParser alloc] initWithData:webData];
parser.delegate = self;
if([parser parse])
{
isParsingStart = NO;
NSLog(@"The XML is Parsed.");
[[self tableView] reloadData];
}
else
{
isParsingStart = NO;
NSLog(@"Failed to parse the XML");
}
[webData setData:nil];
}
- (void)parserDidStartDocument:(NSXMLParser *)parser
{
isParsingStart = YES;
}
- (void)parserDidEndDocument:(NSXMLParser *)parser
{
//do stuff
}
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
{
// do stuff
}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
// do stuff
}
.
.
.