gpt4 book ai didi

swift - swift 中的 “This application is modifying the autolayout engine from a background thread after the engine was accessed from the main thread”

转载 作者:行者123 更新时间:2023-11-28 13:51:27 25 4
gpt4 key购买 nike

我正在尝试获取用户联系人。一切正常,除了当用户点击按钮允许我们访问联系人时,联系人会在控制台中打印出来,但是到另一个 viewcontroller 的 segue 使用了很多时间,我的控制台输出就像疯了似的说:

This application is modifying the autolayout engine from a background thread after the engine was accessed from the main thread

带有堆栈列表...

阅读 StackOverflow 上的错误后,我发现我需要一个 DispatchQueue.main.async()。但是我真的不知道把它放在哪里?有人可以向我解释吗?

这是 action outlet 的代码,当按钮被按下时,错误发生的地方:

@IBAction func didTapFindContacts(_ sender: Any) {
fetchContacts()
}

func fetchContacts() {
contactStore.requestAccess(for: .contacts) { (success, error) in
if let error = error {
print("failed to request access:", error)
return
}

if success {
self.performSegue(withIdentifier: "inviteFriends", sender: nil)
let contactStore = CNContactStore()
let keys = [CNContactGivenNameKey,
CNContactPhoneNumbersKey,
CNContactFamilyNameKey] as [Any]
let request = CNContactFetchRequest(keysToFetch: keys as! [CNKeyDescriptor])
do {
try contactStore.enumerateContacts(with: request){ (contact, stop) in
// Array containing all unified contacts from everywhere

let name = contact.givenName
let familyName = contact.familyName
let number = contact.phoneNumbers.first?.value.stringValue

let contactsAppend = ContactStruct(givenName: name, familyName: familyName, number: number!)
self.contacts.append(contactsAppend)

print(name)
}
} catch {
print("unable to fetch contacts")
}
}
//go to other page
}
}

最佳答案

所有与 UI 相关的代码都需要在主线程上运行。在你的情况下,这是一个 segue

DispatchQueue.main.async { [weak self] in
self?.performSegue(withIdentifier: "inviteFriends", sender: nil)
}

关于swift - swift 中的 “This application is modifying the autolayout engine from a background thread after the engine was accessed from the main thread”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54553421/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com