Swift 中的“在从主线程访问引擎后,此应用程序正在从后台线程修改自动布局引擎”
“This application is modifying the autolayout engine from a background thread after the engine was accessed from the main thread” in Swift
我正在尝试获取用户联系人。一切正常,除了当用户单击按钮允许我们访问联系人时,联系人会在控制台中打印出来,但是到另一个 viewcontroller 的 segue 会花费很多时间,我的控制台输出正在运行像疯了似的说:
This application is modifying the autolayout engine from a background
thread after the engine was accessed from the main thread
带有堆栈列表...
阅读 Whosebug 上的错误后,我发现我需要 DispatchQueue.main.async()。但是我真的不知道把它放在哪里?有人可以向我解释吗?
这是动作出口的代码,当按下按钮时,发生错误的地方:
@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)
}
我正在尝试获取用户联系人。一切正常,除了当用户单击按钮允许我们访问联系人时,联系人会在控制台中打印出来,但是到另一个 viewcontroller 的 segue 会花费很多时间,我的控制台输出正在运行像疯了似的说:
This application is modifying the autolayout engine from a background thread after the engine was accessed from the main thread
带有堆栈列表...
阅读 Whosebug 上的错误后,我发现我需要 DispatchQueue.main.async()。但是我真的不知道把它放在哪里?有人可以向我解释吗?
这是动作出口的代码,当按下按钮时,发生错误的地方:
@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)
}