首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >detachNewThreadSelector随机崩溃应用程序

detachNewThreadSelector随机崩溃应用程序
EN

Stack Overflow用户
提问于 2011-05-09 22:49:28
回答 3查看 794关注 0票数 4

我正在开发一个iphone应用程序,其中我提取RSS feed,然后解析them.My代码,如下所示:

代码语言:javascript
运行
复制
 - (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”。

代码语言:javascript
运行
复制
gdb stack trace at 'putpkt: write failed':
0   gdb-arm-apple-darwin                0x0019026b remote_backtrace_self + 54
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2011-05-16 16:56:04

最后,我使用一个标志来检查视图是否出现(通过在viewdidAppear中将标志设为真),如果视图没有出现,不要运行线程function.That解决了问题!

票数 0
EN

Stack Overflow用户

发布于 2011-05-10 14:15:05

我不确定,但我猜这个方法的参数会在某个时候被释放。您能确保URL出现在方法parseXMLFileAtURL中吗

票数 0
EN

Stack Overflow用户

发布于 2013-05-30 16:11:10

//您可以使用该方法,如果您不需要更新UserInterface,该方法会更安全

代码语言:javascript
运行
复制
 [self performSelectorInBackground:@selector(parseXMLFileAtURL:) withObject:path];

如果UserInterface也需要更新,您可以首先在后台解析数据,然后使用update the ui with方法。

代码语言:javascript
运行
复制
[self performSelectorOnMainThread:@selector(myUpdateUI) withObject:nil waitUntilDone:YES];
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/5938569

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档