Ios 更新UI之前出现的NSURLConnection sendAsynchronousRequest completionHandler日志

Ios 更新UI之前出现的NSURLConnection sendAsynchronousRequest completionHandler日志,ios,objective-c,sendasynchronousrequest,Ios,Objective C,Sendasynchronousrequest,我有这段代码可以向服务器发送json字符串 [NSURLConnection sendAsynchronousRequest:req queue:[[NSOperationQueue alloc] init] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) { if ([data length

我有这段代码可以向服务器发送json字符串

[NSURLConnection
 sendAsynchronousRequest:req
 queue:[[NSOperationQueue alloc] init]
 completionHandler:^(NSURLResponse *response,
                     NSData *data,
                     NSError *error)
 {

     if ([data length] >0 && error == nil)
     {

         NSLog(@"Done");

     }
     else if ([data length] == 0 && error == nil)
     {
         NSLog(@"Nothing was downloaded.");
         self.resultLabel.text=@"Done!";
         self.view.userInteractionEnabled = TRUE;
     }
     else if (error != nil){
         NSLog(@"Error = %@", error);
     }


 }];
异步请求完成得很好,日志几乎在完成后立即显示。但是,该代码:

self.resultLabel.text=@"Done!";
self.view.userInteractionEnabled = TRUE;

需要10秒钟才能在UI中显示。有人知道为什么会发生这种情况吗?

您必须在主线程中执行所有UI更改:

....
if ([data length] == 0 && error == nil) {
    dispatch_async(dispatch_get_main_queue(), ^{
        self.resultLabel.text=@"Done!";
        self.view.userInteractionEnabled = TRUE;
    });
}
....

必须在主线程中执行所有UI更改:

....
if ([data length] == 0 && error == nil) {
    dispatch_async(dispatch_get_main_queue(), ^{
        self.resultLabel.text=@"Done!";
        self.view.userInteractionEnabled = TRUE;
    });
}
....

是的,一定是那样!谢谢救了我一天:)是的,一定是那样!谢谢救了我一天:)