我想执行一项必须在后台运行的任务。该任务涉及解析 Web 服务的响应。由于需要一些时间,我希望它在后台运行。下面是我尝试执行后台任务的代码。
//**When a button is tapped this method will be called.**
dispatch_queue_t myQueue=dispatch_queue_create("My Queue", NULL);
dispatch_async(myQueue, ^{
[self getDataPetioleGraph];//**This will parse the webservice response and the output will be stored in a array for populating on tableview**
dispatch_async(dispatch_get_main_queue(), ^{
[tableViewObj reloadData];
});
});
-(void)getDataPetioleGraph{
NSString *soapContent=[NSString stringWithFormat:@"<?xml version=\"1.0\" encoding=\"utf-8\"?>"
"<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">"
"<soap:Body>"
"<GetDataPetioleGraph xmlns=\"http://tempuri.org/\">"
"<Account_Number>%@</Account_Number>"
"</GetDataPetioleGraph>"
"</soap:Body>"
"</soap:Envelope>",@"38003"];
NSMutableURLRequest *request=[[[NSMutableURLRequest alloc]initWithURL:[NSURL URLWithString:@"web service link is here"]] autorelease];
NSString *contentLength=[NSString stringWithFormat:@"%d",[soapContent length]];
[request addValue:@"text/xml" forHTTPHeaderField:@"Content-Type"];
[request addValue:@"http://tempuri.org/GetDataPetioleGraph" forHTTPHeaderField:@"SOAPAction"];
[request addValue:contentLength forHTTPHeaderField:@"Content-Length"];
[request setHTTPMethod:@"POST"];
[request setHTTPBody:[soapContent dataUsingEncoding:NSUTF8StringEncoding]];
getDPGConnection=[[[NSURLConnection alloc] initWithRequest:request delegate:self] autorelease];
[getDPGConnection start];
但是当我点击调用上述函数的按钮时,什么也没有发生。我已经尝试了其他答案,但我仍然没有得到解决方案。谁能告诉我哪里出错了。提前致谢。