开发者

iPhone app crash while application one thread running async

I am currently working in small chat app in iPhone. I am call to web services; one for data sending, and other for get friends response. I am implementing the code as follows:

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 *)TimeV开发者_运维知识库al
{
// call web service to get response 
// if error occur then timer will be release and nil
}

My app crashes and it throws the following error:

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...

How can I debug why this happens?


THe error message tells you exactly what the problem is. For what it's worth, there's no need to create a background thread and run loop just to start an NSTimer. You may as well just create the NSTimer in your main thread—it won't block it, it'll just sit in the run loop and fire when it needs to. There's no need for -performSelectorInBackground:withObject:.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜