我已经使用CNContact.framework阅读了联系人,如下所示

let contactStore = CNContactStore()
let keys = [CNContactEmailAddressesKey,
            CNContactPhoneNumbersKey,
            CNContactFormatter.descriptorForRequiredKeys(for: .fullName),
            CNContactThumbnailImageDataKey] as! [CNKeyDescriptor]

// The container means
// that the source the contacts from, such as Exchange and iCloud
var allContainers: [CNContainer] = []
do {
     allContainers = try contactStore.containers(matching: nil)

     // Loop the containers
     for container in allContainers {
          let fetchPredicate = CNContact.predicateForContactsInContainer(withIdentifier: container.identifier)
          do {
               let containerResults = try contactStore.unifiedContacts(matching: fetchPredicate, keysToFetch: keys)

               for contact in containerResults {
                     // iterating over contact
               }

               print("Saving into core data completed")

             } catch {
               print("Error fetching results for container")
             }
      }


   } catch {
       print("Error fetching containers")
   }
}

在上面的代码中,我一次阅读了所有联系人。假设我有10000个联系人,并且所有10K联系人都将立即加载到内存中。有什么办法可以通过提供offsetlimit来获取联系人。

说我想从0-100获取联系人,然后从101-200 ...

提前致谢。

最佳答案

只需调用 enumerateContacts(with:usingBlock:) 即可。它一次可以使您与一个联系人联系,因此您可以自由地与他们联系。并且为您的信息块提供了stop参数,以便您可以随时停止接收联系人。

例如,您可以第一次调用它,读取100个联系人,然后停止。然后第二次调用它,跳过前100个联系人(即返回即可继续循环),阅读下100个联系人,然后停止。等等。

关于ios - 逐部阅读联系人(ios),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/49856276/

10-13 09:03