iphone应用程序崩溃,同时应用程序运行一个线程asyc

问题描述:

嗨iam目前在iphone的小聊天应用程序工作iam调用Web服务.one数据发送和其他获取朋友响应,所以iam致谢代码如下。iphone应用程序崩溃,同时应用程序运行一个线程asyc

NSMutableData *webData; 
NSXMLParser *xmlParser; 
NSMutableString *xmlParsingResult; 
NStimer *timer; 
-(IBAction)send:(id)sender 
{ 
//req include web request info 
NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:req delegate:self]; 
    if (conn) 
    { 
    webData = [[NSMutableData data] retain]; 
    } 
} 
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response 
{ 
[webData setLength: 0]; 
} 
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data 
{ 
[webData appendData:data]; 
} 
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error 
{ 
[connection release]; 
[webData release]; 
} 
-(void)connectionDidFinishLoading:(NSURLConnection *)connection 
{ 
xmlParser = [[NSXMLParser alloc] initWithData: webData]; 
[xmlParser setDelegate: self]; 
[xmlParser setShouldResolveExternalEntities: YES]; 
[xmlParser parse]; 
[xmlParser release]; 
[connection release]; 
[webData release]; 
} 

-(void)parser:(NSXMLParser *)xmlparser didStartElement:(NSString *)elementName namespaceURI:(NSString *) namespaceURI qualifiedName:(NSString *)qName attributes: (NSDictionary *)attributeDict 
{ 
if([elementName isEqualToString:@"string"]) 
{ 
    if(!xmlParsingResult) 
    { 
    xmlParsingResult = [[NSMutableString alloc] init] ; 
    } 
} 
} 

-(void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string 
{ 
[xmlParsingResult appendString: string]; 
} 

-(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName 
{ 
if([elementName isEqualToString:@"string"]) 
{ 
    if(timer!=nil) 
    {  
    } 
    else 
    { 
[self performSelectorInBackground:@selector(CallResponse) withObject:nil]; 
    } 
    xmlParsingResult=nil; 
    [xmlParsingResult release]; 
} 
} 
//================================================================================================================== 
-(void)CallResponse 
{ 
NSLog(@"timer start:"); 
NSAutoreleasePool *timerNSPool = [[NSAutoreleasePool alloc] init]; 
timer = [NSTimer scheduledTimerWithTimeInterval:10.0 target:self selector:@selector(Refresh:) userInfo:nil repeats: YES]; 
NSRunLoop* runLoop = [NSRunLoop currentRunLoop]; 
[runLoop run]; 
[timerNSPool release]; 
} 
-(void)Refresh:(NSTimer *)TimeVal 
{ 
// call web service to get response 
// if error occur then timer will be release and nil 
} 

我的应用程序崩溃,并抛出follwing错误

bool _WebTryThreadLock(bool), 0x7089e20: Tried to obtain the web lock from a thread other than the main thread or the web thread. This may be a result of calling to UIKit from a secondary thread. Crashing now... 

PLZ出码PLZ的给我的帮助或代码snnipet提前 感谢

+0

你能格式化你的代码吗?这真的有帮助。 – 2010-10-23 15:57:22

+0

@ user326561我为您编写了代码,下次在您身上。 – Jordan 2010-10-23 16:46:58

错误消息告诉你到底是什么问题是。对于它的价值,无需创建后台线程并运行循环来启动NSTimer。你可以在你的主线程中创建NSTimer--它不会阻塞它,它只会坐在运行循环中并在需要时触发。没有必要为-performSelectorInBackground:withObject: