2013-02-01 48 views
1

我出庭NSURLConnection的sendAsynchronousRequest completionHandler日誌有這樣的代碼,以JSON字符串發送到服務器更新UI

[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."); 
     [email protected]"Done!"; 
     self.view.userInteractionEnabled = TRUE; 
    } 
    else if (error != nil){ 
     NSLog(@"Error = %@", error); 
    } 


}]; 

異步請求完成罰款和日誌幾乎立即將其結束後顯示出來。但是,此代碼:

[email protected]"Done!"; 
self.view.userInteractionEnabled = TRUE; 

需要10秒才能顯示在UI中。任何人都知道爲什麼會發生?

回答

12

你必須在主線程中執行所有的用戶界面的變化:

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

是的,它是這個樣子!謝謝。保存了我的一天:) – Saqib