detachNewThreadSelector随机崩溃应用程序

时间:2011-05-09 14:49:28

标签: iphone objective-c rss threadpool pool

我正在开发一个iphone应用程序,我在其中提取RSS提要然后解析它们。我的代码如下:

 - (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]     [切换到主题12035]     2011-05-10 11:31:30。932年度报告[454:490b]找到文件并开始解析     [切换到主题14339]     2011-05-10 11:32:04。73年度报告[454:640b]找到文件并开始解析     [切换到主题13827]     [切换到主题13827]     程序接收信号:“EXC_BAD_ACCESS”。

gdb stack trace at 'putpkt: write failed':
0   gdb-arm-apple-darwin                0x0019026b remote_backtrace_self + 54

3 个答案:

答案 0 :(得分:0)

我不确定,但我猜这个方法的参数会在某个时候释放。您能否确保方法parseXMLFileAtURL

中存在该URL

答案 1 :(得分:0)

最后,我使用一个标志来检查视图是否出现(通过在viewdidAppear中使标志为true)并且如果视图没有出现,则不要运行线程函数。这解决了问题!!! < / p>

答案 2 :(得分:0)

//您可以使用该方法,如果您不必更新UserInterface

,则更安全
 [self performSelectorInBackground:@selector(parseXMLFileAtURL:) withObject:path];

如果还需要更新UserInterface,您可以先在后台解析数据并使用方法更新ui。

[self performSelectorOnMainThread:@selector(myUpdateUI) withObject:nil waitUntilDone:YES];