2014-04-11 57 views
1

我有兩個UILabel需要以毫秒爲單位進行更新。我跑了我的應用程序,並注意到其中一個UILabel(「第二個UILabel」)更新比第一個更慢。這不是一個很大的區別,但它是可感知的。同時更新兩個UILabel

第二個UILabel在UILabel上顯示其結果之前做了短計算。我懷疑我可能需要將其推送到後臺線程。

我已經在Java中使用線程,但想探索GCD,操作隊列,調度隊列等。我已經閱讀了很多(特別是來自Apple網站),但只是無法理解它們。

所以我需要檢查這裏的大師:

Q1:是第二次的UILabel感知問題,是由於UI線程導致線程爭的更新? Q2:我的應用程序更新第二個UILabel需要後臺線程還是GCD等足夠?

+1

您應該只從主線程更新UI。如果您正在進行任何後臺計算,請確保將您的UI代碼封裝在一個塊中並將其分派到主隊列中。 – Mercurial

+0

你會有示例代碼顯示嗎? – Poliquin

+0

你提供了一個有效的答案:) – Mercurial

回答

1

答案。

dispatch_queue_t background_queue = dispatch_queue_create("label", NULL); 

dispatch_async(background_queue, ^{ 
    // do some stuff that takes a long time here... 

    // follow up with some stuff on the main queue 
    dispatch_async(dispatch_get_main_queue(), ^{ 
     // Typically updating the UI on the main thread. 

    }); 
});