我正在开发一个iphone应用程序,其中我提取RSS feed,然后解析them.My代码,如下所示:
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
NSLog(@"in view did appear");
if ([stories count] == 0) {
NSString * path = @"http://www.shire.com/shireplc/rss.jsp";
//[self parseXMLFileAtURL:path];
//[self performSelectorInBackground:@selector(parseXMLFileAtURL:) withObject:path];
NSLog(@"internet is %d",[self checkInternet]);
if([self checkInternet]==1)
[NSThread detachNewThreadSelector:@selector(parseXMLFileAtURL:)
toTarget:self withObject:path];
}
}
- (void)parseXMLFileAtURL:(NSString *)URL
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
stories = [[NSMutableArray alloc] init];
//you must then convert the path to a proper NSURL or it won't work
NSURL *xmlURL = [NSURL URLWithString:URL];
// here, for some reason you have to use NSClassFromString when trying to alloc NSXMLParser, otherwise you will get an object not found error
// this may be necessary only for the toolchain
rssParser = [[NSXMLParser alloc] initWithContentsOfURL:xmlURL];
// Set self as the delegate of the parser so that it will receive the parser delegate methods callbacks.
[rssParser setDelegate:self];
// Depending on the XML document you're parsing, you may want to enable these features of NSXMLParser.
[rssParser setShouldProcessNamespaces:NO];
[rssParser setShouldReportNamespacePrefixes:NO];
[rssParser setShouldResolveExternalEntities:NO];
[rssParser parse];
[pool release];
}
有没有人能告诉我我哪里错了?我的日志如下:日志:切换到线程12803 2011-05-10 11:31:30.932年度报告454:490b找到文件并开始解析切换到线程14339 2011-05-10 11:32:04.742年度报告454:640b找到文件并开始解析切换到线程13827程序接收信号:“EXC_BAD_ACCESS”。
gdb stack trace at 'putpkt: write failed':
0 gdb-arm-apple-darwin 0x0019026b remote_backtrace_self + 54
发布于 2011-05-16 16:56:04
最后,我使用一个标志来检查视图是否出现(通过在viewdidAppear中将标志设为真),如果视图没有出现,不要运行线程function.That解决了问题!
发布于 2011-05-10 14:15:05
我不确定,但我猜这个方法的参数会在某个时候被释放。您能确保URL出现在方法parseXMLFileAtURL
中吗
发布于 2013-05-30 16:11:10
//您可以使用该方法,如果您不需要更新UserInterface,该方法会更安全
[self performSelectorInBackground:@selector(parseXMLFileAtURL:) withObject:path];
如果UserInterface也需要更新,您可以首先在后台解析数据,然后使用update the ui with方法。
[self performSelectorOnMainThread:@selector(myUpdateUI) withObject:nil waitUntilDone:YES];
https://stackoverflow.com/questions/5938569
复制相似问题