我有一个异步子类完成,并在完成后等待30秒,如果你看到,nslog显示OK答案,但它等待太长时间来放置在NSLOG中显示的相同文本,进入UILabel ...... 我怎么能打破这个? 这是代码:
-(BOOL) setParams{
response.text = @"";
//NSLog(@"Start:\r\n");
[indicator setHidden:NO];
[indicator startAnimating];
if(pngData != nil){
request = [NSMutableURLRequest new];
request.timeoutInterval = 20.0;
[request setURL:[NSURL URLWithString:URL]];
[request setHTTPMethod:@"POST"];
NSString *boundary = @"---------------------------14737809831466499882746641449";
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
[request addValue:contentType forHTTPHeaderField: @"Content-Type"];
[request setValue:@"text/html,<span id=\"IL_AD4\" class=\"IL_AD\">application</span>/xhtml+xml,application/xml;q=0.9,*/*;q=0.8" forHTTPHeaderField:@"Accept"];
[request setValue:@"Mozilla/5.0 (Macintosh; Intel <span id=\"IL_AD10\" class=\"IL_AD\">Mac OS X</span> 10_7_5) AppleWebKit/536.26.14 (KHTML, like Gecko) Version/6.0.1 Safari/536.26.14" forHTTPHeaderField:@"User-Agent"];
NSMutableData *body = [NSMutableData data];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
//Cada campo------------------------------------------------------------------------------------
NSString *keyName = @"add_product";
NSString *keyValue = @"valRichard";
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"\r\n\r\n",keyName] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"%@",keyValue] dataUsingEncoding:NSUTF8StringEncoding]];
//--------------------------------------------------------------------------------------------
//----La Imagen
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
NSString *imagePostName = @"imageToUpload";
NSString *imagePostValue = @"x-cosa.png";
[body appendData:[[NSString stringWithFormat:@"Content-Disposition:form-data; name=\"%@\"; filename=\"%@\"\r\n",imagePostName, imagePostValue] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"Content-Type: application/octet-stream\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[NSData dataWithData:pngData]];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
//---------------------------------------------------------------------------------------------
[request setHTTPBody:body];
[request addValue:[NSString stringWithFormat:@"%lu", (unsigned long)[body length]] forHTTPHeaderField:@"Content-Length"];
return TRUE;
}else{
response.text = NO_IMAGE;
return FALSE;
}
}
- (IBAction) uploadImageAsync1:(id)sender
{
[indicator setHidden:NO];
if([self setParams]){
response.text = @"uploadImageAsync1";
NSOperationQueue *queue = [[NSOperationQueue alloc]init];
[NSURLConnection sendAsynchronousRequest:request queue:queue
completionHandler:^(NSURLResponse *urlResponse, NSData *data, NSError *error){
NSString *theAnswer =[NSString stringWithFormat:@"Finalizado: %@",[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]];
NSLog(@"%@",theAnswer);
//HERE IS THE CODE WHICH TAKES LIKE A MINUTE TO EXECUTE!!!!!
[indicator stopAnimating];
[indicator setHidden:YES];
response.text = theAnswer;
//END OF THE THREE LINES THAT EXECUTES AFTER
[UIApplication sharedApplication].networkActivityIndicatorVisible = FALSE;
if (error) {
NSLog(@"error:%@", error.localizedDescription);
}
}];
}
}
答案 0 :(得分:1)
UILabel是UIKit的一部分,它不是线程安全的,只能从主线程访问。 queue
中的sendAsynchronousRequest:
参数是调用完成处理程序的队列,因此您应该传递它[NSOperationQueue mainQueue]
,以便在主线程上调用完成处理程序。