我的函数执行需要时间
func hasValidValues() -> Bool{
let leftValue = Double(leftTextValue) ?? 0
let rightValue = Double(rightTextValue) ?? 0
switch self.stackViewType {
let leftValid = self.hasValidValue(min: targetMin, max: targetMax, value: CGFloat(leftValue), unitConversation: true)
let rightValid = self.hasValidValue(min: 0, max: plusMinusLimit, value: CGFloat(rightValue), unitConversation: true)
return leftValid && rightValid
}
现在我需要在后台线程中执行它,并希望返回以便 UI 线程可以访问它并且我可以在另一个 Controller 中更改 UI。
谁能帮我解决这个问题?
最佳答案
使用DispatchQueue
:
DispatchQueue.global().async {
let result = self.hasValidChanges()
DispatchQueue.main.async {
// Use result
}
}
关于ios - 从后台线程返回函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52663020/