使用联系人框架缓慢获取联系人
Slow contact fetching using Contacts framework
我正在使用 Xcode 7.3 和 Swift 2.2。我正在尝试获取设备的所有联系人并将其存储在一个数组中。它在模拟器上运行良好,但在设备(有 378 个触点)上测试时速度非常慢。这样做大约需要 20-25 秒。我放置了各种断点并注意到从 phone 中获取联系人需要最长时间。使用 table 视图显示创建的数组中的联系人根本不需要时间。这是我的代码,
var results: [CNContact] = []
func retrieveContactsWithStore() {
let contactStore = CNContactStore()
var i = 0
var allContainers: [CNContainer] = []
do {
allContainers = try contactStore.containersMatchingPredicate(nil)
} catch {
print("Error fetching containers")
}
for container in allContainers {
let fetchPredicate = CNContact.predicateForContactsInContainerWithIdentifier(container.identifier)
do {
let containerResults = try contactStore.unifiedContactsMatchingPredicate(fetchPredicate, keysToFetch: [
CNContactGivenNameKey, CNContactFamilyNameKey, CNContactPhoneNumbersKey])
results.appendContentsOf(containerResults)
} catch {
print("Error fetching results for container")
}
}
下面是我填充 table 视图的方式。
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath)
cell.textLabel?.text = results[indexPath.row].givenName
cell.detailTextLabel?.text = ((results[indexPath.row].phoneNumbers[0].value as? CNPhoneNumber)?.valueForKey("digits") as? String)
return cell
}
在调试导航器中,CPU 也达到了 98-99%,能量影响 - 非常高。一旦获取了联系人,这些 return 将变为正常值。
事实证明,获取 CNContactPhoneNumbersKey 需要花费很多时间。只需获取 CNContactGivenNameKey 即可。(2-3 秒)。我们对此有解决方案吗?
框架和代码都没有问题。我在其他设备上测试了代码,它运行良好。它能够在不到一秒的时间内获取近 900 个联系人。
所以问题是特定于设备的,重新安装 iOS 并将设备恢复为新设备 iPhone 解决了问题。
如果其他人遇到类似问题,只需备份您的重要内容(例如-联系人、照片等)并重新安装 iOS 并将设备恢复为新设备 iPhone。
我正在使用 Xcode 7.3 和 Swift 2.2。我正在尝试获取设备的所有联系人并将其存储在一个数组中。它在模拟器上运行良好,但在设备(有 378 个触点)上测试时速度非常慢。这样做大约需要 20-25 秒。我放置了各种断点并注意到从 phone 中获取联系人需要最长时间。使用 table 视图显示创建的数组中的联系人根本不需要时间。这是我的代码,
var results: [CNContact] = []
func retrieveContactsWithStore() {
let contactStore = CNContactStore()
var i = 0
var allContainers: [CNContainer] = []
do {
allContainers = try contactStore.containersMatchingPredicate(nil)
} catch {
print("Error fetching containers")
}
for container in allContainers {
let fetchPredicate = CNContact.predicateForContactsInContainerWithIdentifier(container.identifier)
do {
let containerResults = try contactStore.unifiedContactsMatchingPredicate(fetchPredicate, keysToFetch: [
CNContactGivenNameKey, CNContactFamilyNameKey, CNContactPhoneNumbersKey])
results.appendContentsOf(containerResults)
} catch {
print("Error fetching results for container")
}
}
下面是我填充 table 视图的方式。
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath)
cell.textLabel?.text = results[indexPath.row].givenName
cell.detailTextLabel?.text = ((results[indexPath.row].phoneNumbers[0].value as? CNPhoneNumber)?.valueForKey("digits") as? String)
return cell
}
在调试导航器中,CPU 也达到了 98-99%,能量影响 - 非常高。一旦获取了联系人,这些 return 将变为正常值。
事实证明,获取 CNContactPhoneNumbersKey 需要花费很多时间。只需获取 CNContactGivenNameKey 即可。(2-3 秒)。我们对此有解决方案吗?
框架和代码都没有问题。我在其他设备上测试了代码,它运行良好。它能够在不到一秒的时间内获取近 900 个联系人。
所以问题是特定于设备的,重新安装 iOS 并将设备恢复为新设备 iPhone 解决了问题。
如果其他人遇到类似问题,只需备份您的重要内容(例如-联系人、照片等)并重新安装 iOS 并将设备恢复为新设备 iPhone。