detachNewThreadSelector 使应用程序随机崩溃

发布于 2024-11-06 10:54:16 字数 2004 浏览 2 评论 0原文

我正在开发一个 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.742 年报[454:640b] 找到文件并开始解析 [切换到线程13827] [切换到线程13827] 程序收到信号:“EXC_BAD_ACCESS”。

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

I am developing an iphone app in which I am extracting RSS feeds and then parsing them.My code is as bellow:

 - (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];

}

Can anyone tell me where I am going wrong?
My log is as bellow:
log: [Switching to thread 12803]
[Switching to thread 12035]
2011-05-10 11:31:30.932 Annual Report[454:490b] found file and started parsing
[Switching to thread 14339]
2011-05-10 11:32:04.742 Annual Report[454:640b] found file and started parsing
[Switching to thread 13827]
[Switching to thread 13827]
Program received signal: “EXC_BAD_ACCESS”.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

故人爱我别走 2024-11-13 10:54:16

我不确定,但我猜该方法的参数会在某个时刻被释放。您能否确保该 URL 存在于方法 parseXMLFileAtURL

I am not sure but i guess the parameter for the method gets released at some point. Can you make sure that the URL is present in the method parseXMLFileAtURL

关于从前 2024-11-13 10:54:16

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

Finally,I used a flag to check if the view is appeared or not(by making flag true in viewdidAppear) and if the view is not appearing,don't run the thread function.That solved the problem!!!

诗酒趁年少 2024-11-13 10:54:16

// 你可以使用该方法,如果你不必更新UserInterface,这会更安全。

 [self performSelectorInBackground:@selector(parseXMLFileAtURL:) withObject:path];

如果UserInterface也需要更新,你可以先在后台解析数据,然后用方法更新ui。

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

// you can use the method, which is safer if your have not to update the UserInterface

 [self performSelectorInBackground:@selector(parseXMLFileAtURL:) withObject:path];

if UserInterface needs also to be updated you can first parse data in background and the update the ui with method.

[self performSelectorOnMainThread:@selector(myUpdateUI) withObject:nil waitUntilDone:YES];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文