Iphone detachNewThreadSelector随机使应用程序崩溃
我正在开发一个iphone应用程序,在其中提取RSS提要并对其进行解析。我的代码如下:Iphone detachNewThreadSelector随机使应用程序崩溃,iphone,objective-c,rss,threadpool,pool,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.j
- (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\U坏访问”
gdb stack trace at 'putpkt: write failed':
0 gdb-arm-apple-darwin 0x0019026b remote_backtrace_self + 54
我不确定,但我猜该方法的参数在某个时候会被释放。你能确保URL在方法
ParseXmlFileAtrol
中存在吗最后,我使用了一个标志来检查视图是否出现(通过在ViewDidDisplay中使标志为true),如果视图没有出现,就不要运行线程函数。这就解决了问题 //您可以使用该方法,如果您不需要更新用户界面,则该方法更安全
[self performSelectorInBackground:@selector(parseXMLFileAtURL:) withObject:path];
如果还需要更新UserInterface,您可以首先在后台解析数据,然后使用方法更新ui
[self performSelectorOnMainThread:@selector(myUpdateUI) withObject:nil waitUntilDone:YES];
当您崩溃时,您从控制台获得了什么日志?另外,请在打开断点的情况下运行,并告诉我们当应用程序崩溃时您在调试器中看到的堆栈跟踪,以便我们确定原因。我打赌这是在一个NSXMLParser委托回调中,您在这里没有显示,但没有更多信息,我们无法诊断。实际上,我应该解释整个场景。我正在使用stringWithFormat将获取的所有提要附加到字符串中。当我此时观察到分配时,它们的速度超过34 MB,但应用程序没有崩溃。当我打开应用程序时,应用程序开始搜索源,当它在web视图中加载源时,分配的内存超过30 MB。但应用程序没有崩溃。然后,当我在应用程序中导航时,解析器再次进入parserDidStartDocument:然后应用程序抛出一个错误的访问异常,这是我得到的唯一日志。Aat这次应用程序崩溃。为什么这个错误的访问和额外的内存使用?为什么它进入parserDidStartDocument:第二次?我猜线程处理中有一些问题。但是什么?我已经编辑了问题以添加控制台的日志url存在,我可以在浏览器中访问它。我还收到了提要从它。