gpt4 book ai didi

ios - 使用用户 NSContacts 查找 friend 页面非常慢 + "This application is modifying the autolayout engine from a background thread"

转载 作者:行者123 更新时间:2023-11-29 01:00:27 26 4
gpt4 key购买 nike

我正在尝试开发一个查找好友页面,用于检查用户的任何联系人是否已加入我的平台。它不应该花太长时间,但是大约需要 2 到 3 分钟,这让我觉得它有问题。我对那可能是什么有一些想法:

  • 我在异步调用的某个地方搞砸了。这是我的主要假设,虽然我不确定在哪里。这很可能是原因,因为在控制台上我收到以下错误:

This application is modifying the autolayout engine from a background thread, which can lead to engine corruption and weird crashes. This will cause an exception in a future release.

我认为这是 hapenning 在一个 for 循环中,因为我多次收到错误。

  • 我的另一个想法是延迟与约束有关,因为我也得到:

将尝试通过打破约束来恢复 NSLayoutConstraint:0x127c2ab00 UITextView:0x127011000'Name lastname created ...'.centerY == UIView:0x1278c21b0.centerY>

            let store = CNContactStore()

//Find all the contacts and store them in an instance variable

store.requestAccessForEntityType(.Contacts) { granted, error in
guard granted else {
dispatch_async(dispatch_get_main_queue()) {
// user didn't grant authorization, so tell them to fix that in settings
print(error)
}
return
}

// get the contacts
self.showActivityIndicator()

var localcontacts = [CNContact]()
let request = CNContactFetchRequest(keysToFetch: [CNContactIdentifierKey, CNContactFormatter.descriptorForRequiredKeysForStyle(.FullName), CNContactPhoneNumbersKey])

request.sortOrder = CNContactSortOrder.GivenName

do {
try store.enumerateContactsWithFetchRequest(request) { contact, stop in
if !contact.phoneNumbers.isEmpty {
localcontacts.append(contact)
}

}
} catch {
print(error)
}

self.cncontacts = localcontacts

var cs = [contactObject]()

for contact in self.cncontacts {
let formatter = CNContactFormatter()
formatter.style = .FullName
let name = formatter.stringFromContact(contact)
var numbers = [String]()
if (contact.isKeyAvailable(CNContactPhoneNumbersKey)) {
for phoneNumber:CNLabeledValue in contact.phoneNumbers {
let a = phoneNumber.value as! CNPhoneNumber
let number = a.stringValue.stringByReplacingOccurrencesOfString(" ", withString: "-", options: NSStringCompareOptions.LiteralSearch, range: nil)
numbers.append(number)
}
}
if numbers.count > 0 {
let c : contactObject = contactObject.init()
c.name = name!
c.numbers = numbers
cs.append(c)

}

}


self.contacts = cs
//For each contactObject check if there's an user associated with it

for localcontact in self.contacts {



print(localcontact.name)

for number in localcontact.numbers {

print(number)

if number.hasPrefix("+1") || number.hasPrefix("(") || number.hasPrefix("1"){
//Is american phone number
var formattednumber = number

if number.hasPrefix("+1") {

if number.hasPrefix("+1 "){
formattednumber = number.stringByReplacingOccurrencesOfString("+1 ", withString: "")
formattednumber = formattednumber.stringByReplacingOccurrencesOfString(" ", withString: "")
}
else if number.hasPrefix("+1-"){
formattednumber = number.stringByReplacingOccurrencesOfString("+1-", withString: "")
}
else {
formattednumber = number.stringByReplacingOccurrencesOfString("+1", withString: "")
}



}

else if number.hasPrefix("1"){
if number.hasPrefix("1-") {
formattednumber = number.stringByReplacingOccurrencesOfString("1-", withString: "")
}
else{
formattednumber = number.stringByReplacingOccurrencesOfString("1 ", withString: "")
}
}

formattednumber = formattednumber.stringByReplacingOccurrencesOfString(" ", withString: "-")

print(formattednumber)
let userquery = PFUser.query()
userquery?.whereKey("phone", equalTo: formattednumber)
userquery?.getFirstObjectInBackgroundWithBlock({ (user, error) in
if error == nil {
if let user = user as? PFUser {

localcontact.userid = user.objectId!


}
}
})



}


}



}

self.hideActivityIndicator()
let encodedData = NSKeyedArchiver.archivedDataWithRootObject(self.contacts)
userDefaults.setObject(encodedData, forKey: "contacts")
userDefaults.synchronize()
self.tableView.reloadData()


}

如果您对我可能做错的地方有任何建议或想法,请告诉我,我将不胜感激。提前致谢。

最佳答案

我的猜测是

self.showActivityIndi​​cator()

self.hideActivityIndi​​cator()

self.tableView.reloadData()

都试图修改 UI 的布局,因此出现警告。

尝试在

dispatch_async(dispatch_get_main_queue()) {

block

关于ios - 使用用户 NSContacts 查找 friend 页面非常慢 + "This application is modifying the autolayout engine from a background thread",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37059946/

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