如何检查通讯录中的特定联系人是否已更新并在应用程序中反映出来?

时间:2018-07-04 10:28:42

标签: ios iphone swift contacts

在应用程序中,我正在从通讯录中获取联系人,并在更新联系人后将其存储在领域db。这是从通讯录中获取所有联系人的代码。

func getAllContactsFromAddressBook() {
    do {
        let contactsFetchRequest = CNContactFetchRequest(keysToFetch: [CNContactGivenNameKey as NSString,
                                                                       CNContactFamilyNameKey as NSString,
                                                                       CNContactImageDataKey as NSString,
                                                                       CNContactImageDataAvailableKey as NSString,
                                                                       CNContactPhoneNumbersKey as NSString,
                                                                       CNContactEmailAddressesKey as NSString,
                                                                       CNContactOrganizationNameKey as NSString,
                                                                       CNContactJobTitleKey as NSString,
                                                                       CNContactSocialProfilesKey as NSString ])
        try store.enumerateContacts(with: contactsFetchRequest, usingBlock: { (cnContact, error) in

        if cnContact.givenName == "", cnContact.familyName == "", cnContact.emailAddresses.count == 0 {
        } else {
            if realmStore.findContact(byId: cnContact.identifier) == nil {
                let contact = Contact()
                contact.id = cnContact.identifier
                _ = realmStore.create(contact: contact)
            }
            if let contact = realmStore.findContact(byId: cnContact.identifier) {
                do {
                    try realmStore.write {
                        contact.firstName = cnContact.givenName
                        contact.lastName = cnContact.familyName
                        contact.phoneNumber.removeAll()
                        contact.phoneNumber.append(objectsIn: cnContact.phoneNumbers.map({
                            let p = PhoneNumber()
                            p.phoneNumber = $0.value.stringValue
                            return p
                        }))
                        contact.email.removeAll()
                        contact.email.append(objectsIn: cnContact.emailAddresses.map({
                            let e = Email()
                            let email = $0.value as String
                            e.email = email == "" ? nil : email
                            return e
                        }))
                        contact.organization = cnContact.organizationName
                        contact.jobTitle = cnContact.jobTitle
                        contact.fromLinkedIn = cnContact.socialProfiles.filter({ $0.value.service == "LinkedIn" ? true : false }).first != nil ? true : false
                        if cnContact.imageDataAvailable == true {
                            contact.profileImageBlob = cnContact.imageData
                        }
                    }

                    contact.updatePriortyScore()
                }
                catch {
                    print("Couldn't update contact")
                }
            }




        }
    })
} catch {
    print(error)
}
}

获取联系人的工作正常,但是每次加载视图时,它都会从地址簿中获取所有联系人列表,这会花费一些时间,因此有一种方法可以通知我特定的联系人(已更改的地址)或是否已更改新的联系人已添加到地址簿,以便可以更新领域列表。

0 个答案:

没有答案