2010-10-23 134 views
0

嗨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

回答

1

錯誤消息告訴你到底是什麼問題是。對於它的價值,無需創建後臺線程並運行循環來啓動NSTimer。你可以在你的主線程中創建NSTimer--它不會阻塞它,它只會坐在運行循環中並在需要時觸發。沒有必要爲-performSelectorInBackground:withObject: