将地址簿中的电话号码与Firestore匹配

我正在尝试快速比较Firestore中的电话号码列表并将其与用户的通讯录进行匹配。这行得通,但速度非常慢,尤其是来自Firestore的数字更多时,我认为逻辑上都是错误的,需要修复。

// Array of Strings containing all the phone numbers that are registered on the app are passed through the function.

  func searchForContactUsingPhoneNumber(phoneNumberArray: [String]) -> [CNContact] {
    var result: [CNContact] = []
    let phoneNumberKit = PhoneNumberKit()

    // go through all contacts in the address book for contact in self.contacts {

        if !contact.phoneNumbers.isEmpty {

         let parsedNumberArray = try? phoneNumberKit.parse(phoneNumberArray, withRegion: "GB",  ignoreType: true)

         for singleNumber in parsedNumberArray! {

            let databaseNumber = singleNumber.nationalNumber

            //go through every number of each contach
            for phoneNumber in contact.phoneNumbers {
                let fulMobNumVar  = phoneNumber.value
                let number = fulMobNumVar.value(forKey: "digits") as? String

                let parsedNumber = try? phoneNumberKit.parse(number!, withRegion: "GB", ignoreType: true)
                let contactNumber = parsedNumber?.nationalNumber

                //compare phoneNumber of contact with given users phone number
               if contactNumber == databaseNumber {
                    result.append(contact)
                }

            }
        }
    }
    }

    return result
}
评论