-1
我有一個UIPickerView,一旦選擇了特定的行,就會觸發執行很多計算。如果用戶等待計算完成,顯示計算結果的UILabel(txtRunwayDest)將在約5秒內成功刷新(計算正在進行)。爲了保護應用程序免於用戶在進行計算的同時繼續旋轉UIPicker(這可能會導致顯着的延遲),我決定在選擇一行時顯示帶有旋轉活動指示器的彈出視圖。我把計算放在後臺線程上。Swift 3:在UIPickerView選擇和UILabel刷新之間的滯後
@IBOutlet weak var txtRunwayDest: UILabel!
let pckRunwayDest = UIPickerView()
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int)
{
if (pickerView == self.pckRunwayDest)
{
print("CHECK 0")
let alert = UIAlertController(title: nil, message: "Please wait...", preferredStyle: .alert)
alert.view.tintColor = UIColor.black()
let loadingIndicator: UIActivityIndicatorView = UIActivityIndicatorView(frame: CGRect(x: 10, y: 5, width: 50, height: 50)) as UIActivityIndicatorView
loadingIndicator.hidesWhenStopped = true
loadingIndicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyle.gray
loadingIndicator.startAnimating();
alert.view.addSubview(loadingIndicator)
present(alert, animated: true, completion: nil)
DispatchQueue.global(attributes: .qosBackground).async {
... // CODE
if (row == 0)
{
... // CODE
}
else
{
... // CODE
}
DispatchQueue.main.sync {
print("CHECK DISPLAY")
// Refresh all values on screen
self.displayValues()
// Dismiss the popup view
self.dismiss(animated: false, completion: nil)
}
}
}
}
func displayValues()
{
if (someValue == nil)
{
self.txtRunwayDest.text = "Not selected";
}
else
{
print("START SHOWING TEXT")
self.txtRunwayDest.text = appSingleton.runwayDestShared!.0 + " (" + appSingleton.runwayDestShared!.2 + ")"
print("END SHOWING TEXT")
}
}
所以,一切工作除了txtRunwayDest文本出現的所有計算已作出和UIView的已被解僱約5-10秒後,屏幕上的罰款。控制檯在主線程啓動後立即顯示「END SHOWING TEXT」,但正如我所說的,標籤只有在顯着延遲後纔會改變。
任何想法?謝謝!
應該是'DispatchQueue.main.sync()' –
不,同樣的事! –
不一樣,你需要同步更新UI –