简体   繁体   English

我该如何解决:UILabel.text 只能在主线程中使用

[英]How do I solve this: UILabel.text must be used from main thread only

So I am trying to update the UI in my application.所以我正在尝试更新我的应用程序中的 UI。 What happens is that the labels get updated but when they do not keep their own value but sometimes take the value of the other label.发生的情况是标签得到更新,但是当它们不保留自己的值但有时会采用其他 label 的值时。 Also, I get the message that the label should be used from main thread only, how do I solve this?另外,我收到消息说 label 只能从主线程使用,我该如何解决?

Here is my code:这是我的代码:

let queue = DispatchQueue(label: "update")
queue.async {
   for s in self.fietshare.stands {
      for b in s.bikes {
         lbAvailable.text = "Available Bikes: " + String(s.id) +  "  " + String( s.bikes.count) // shows too big numbers
         nrOfAvailable.text =  String(b.distance) + "M"
         annotationView.layoutIfNeeded()
         print(s.id)
         print("Nr of bikes")
         print(s.bikes.count)
      }
   }
}

DispatchQueue.main.async {
   self.view.backgroundColor = . black;
   self.view.layoutIfNeeded()
}

if (Thread.current.isMainThread) {
    print("Main thread")
}

Try this尝试这个

let queue = DispatchQueue(label: "update")

queue.async {

    var available = ""
    var nrOfAvailable = ""
    for s in self.fietshare.stands{
        for b in s.bikes {
            available = available + "Available Bikes: " + String(s.id) +  "  " + String( s.bikes.count) // shows too big numbers
            nrOfAvailable = String(b.distance) + "M"

            print(s.id)
            print("Nr of bikes")
            print(s.bikes.count)
        }
    }

    // UPDATE UI after all calculations have been done
    DispatchQueue.main.async {
        lbAvailable.text = available
        nrOfAvailable.text = nrOfAvailable
        annotationView.layoutIfNeeded()

        self.view.backgroundColor = . black;
        self.view.layoutIfNeeded()
    }
}

Explanation:解释:

  • you start an asynchronous task with queue.async你用queue.async启动一个异步任务
  • inside this "job in the background" you start a for loop在这个“后台作业”中,您启动了一个 for 循环
  • this for loop needs to update some UI bits这个 for 循环需要更新一些 UI 位
  • do this in the Main Thread, that's why we switch that part back to the Main Thread在主线程中执行此操作,这就是我们将该部分切换回主线程的原因

Explanation 2:解释二:

  • UIKit is not Thread Safe. UIKit 不是线程安全的。 That means that is expecting all changes to come from the Main Thread and be synchronous (one after the other).这意味着期望所有更改都来自主线程并且是同步的(一个接一个)。 So no "locking mechanism" or protection is in place.所以没有“锁定机制”或保护措施到位。 If you try to change a Label from different threads at the same time, weird things can happen due to race conditions, like parts of the label being changed, in different order, etc.如果您尝试同时从不同的线程更改 Label,由于竞争条件可能会发生奇怪的事情,例如 label 的某些部分被更改,顺序不同等。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ImageView 只能在主线程中使用 - ImageView must be used from main thread only 如何从不同的 swift 文件更改 UILabel.text? - How can I change UILabel.text from different swift file? 我收到此错误 [UITextView insertText:] 必须仅从主线程使用 - 我该如何解决? - I am getting this error [UITextView insertText:] must be used from main thread only - How can I resolve it? 如何在其他控制器中使用 UIlabel.text? - How can I use UIlabel.text in other controller? Swift线程1:当我尝试更改uilabel.text时为EXC_BAD_INSTRUCTION - Swift Thread 1: EXC_BAD_INSTRUCTION when I try to change a uilabel.text 更新CloudKit记录并获取“UITextField.text必须仅从主线程使用” - Updating CloudKit record and get “UITextField.text must be used from main thread only” Swift-stopAnimating()-必须仅在主线程中使用 - Swift - stopAnimating() - must be used from main thread only UIViewController.navigationController 只能在主线程中使用 - UIViewController.navigationController must be used from main thread only Swift UIApplication.delegate 只能在主线程中使用 - Swift UIApplication.delegate must be used from main thread only UIViewController.storyboard 必须仅在主线程中使用 - 新错误我不知道如何修复? - UIViewController.storyboard must be used from main thread only - New Error I don't know how to fix?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM