iPhone应用程序在应用程序单线程异步运行时崩溃
我目前正在iPhone上的小型聊天应用程序中工作。我被呼叫到web服务;一个用于发送数据,另一个用于获取好友响应。我正在执行以下代码:iPhone应用程序在应用程序单线程异步运行时崩溃,iphone,Iphone,我目前正在iPhone上的小型聊天应用程序中工作。我被呼叫到web服务;一个用于发送数据,另一个用于获取好友响应。我正在执行以下代码: NSMutableData *webData; NSXMLParser *xmlParser; NSMutableString *xmlParsingResult; NStimer *timer; -(IBAction)send:(id)sender { //req include web request info NSURLConnection *c
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
}
我的应用程序崩溃,并引发以下错误:
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...
如何调试发生这种情况的原因?错误消息会准确地告诉您问题所在。值得一提的是,不需要创建后台线程并运行循环来启动
NSTimer
。您也可以只在主线程中创建NSTimer
,它不会阻止它,它只会在需要时处于运行循环中并启动。不需要-performSelectorInBackground:withObject:
请格式化您的代码。这真的很有帮助。@user326561我为您格式化了代码,下次它在您身上。