1

I have this code to send a json string to a server

[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);
     }


 }];

The asynchronous request finishes fine and the logs show up almost immediately after it is finished. However, this code:

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

Takes a good 10 seconds to show up in the UI. Anyone know why this would happen?

4

1 回答 1

12

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

....
if ([data length] == 0 && error == nil) {
    dispatch_async(dispatch_get_main_queue(), ^{
        self.resultLabel.text=@"Done!";
        self.view.userInteractionEnabled = TRUE;
    });
}
....
于 2013-02-01T16:37:30.243 回答