繁体   English   中英

使用Contacts框架缓慢联系人获取

[英]Slow contact fetching using Contacts framework

我正在使用Xcode 7.3和Swift 2.2。 我试图获取设备的所有联系人并将其存储在一个数组中。 它在模拟器上工作正常但在设备上测试时非常慢(有378个联系人)。 这需要大约20-25秒。 我提出了各种断点,并注意到从手机中取出联系人需要花费最多时间。 使用表视图显示已创建阵列中的联系人根本不需要时间。 这是我的代码,

 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")
        }
    }

这就是我填充表格视图的方式。

     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
}

在Debug Navigator中,CPU也达到了98-99%,Energy Impact - Very High。 一旦获取了联​​系人,这些将返回正常值。

事实证明,获取CNContactPhoneNumbersKey需要花费很多时间。 只需获取CNContactGivenNameKey即可。(2-3秒)。 我们有解决方案吗?

框架或代码没有任何问题。 我在其他设备上测试了代码,并且工作正常。 它能够在不到一秒的时间内获得接近900个联系人。

所以问题是特定于设备并重新安装iOS并在新iPhone解决问题时恢复设备。

如果其他人面临类似问题,只需备份您的重要内容(例如联系人,照片等)并重新安装iOS并将设备恢复为新iPhone。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM