tags:

views:

28

answers:

1

hi iam currently working in small chat app in iphone iam call to web services .one for data sending and other for get friends response so iam impelment code are 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 *)TimeVal
{
// call web service to get response 
// if error occur then timer will be release and nil
}

my app crash and it throw follwing 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...

plz out of code plz give me help or code snnipet thanks in advance

A: 

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

John Calsbeek