libPhoneNumber-iOS icon indicating copy to clipboard operation
libPhoneNumber-iOS copied to clipboard

"isValidNumber" is no longer thread safe as of 0.9.12

Open briankracoff opened this issue 7 years ago • 1 comments

Tested on 0.9.12 and 0.9.13 and both crash, but 0.9.10 does not have this crash

We’re fetching the user’s contacts on a background thread and then parsing out phone numbers using libPhoneNumber. However, we have been unable to upgrade to 0.9.12+ because isValidNumber is no longer thread safe.

Here’s a simplified version of what we’re doing (simulating parsing 1000 phone numbers on a background thread). Running this in a test project crashes.

    func parsePhoneNumbers() {
        for _ in 0 ... 1000 {
            DispatchQueue.global(qos: .background).async {
                _ = self.parsePhoneNumber(string: "1231231234")
            }
        }
    }

    func parsePhoneNumber(string: String) -> NBPhoneNumber? {
        if let phoneNumber = try? NBPhoneNumberUtil.sharedInstance().parse(string, defaultRegion: "US"), NBPhoneNumberUtil.sharedInstance().isValidNumber(phoneNumber) {
            return phoneNumber
        }

        return nil
    }

Are we required to run libPhoneNumber on a main thread now? In the past, we moved it to a background thread for performance reasons because there's no need to hog the main thread while parsing a user's 10k contacts' phone numbers.

Thanks!

briankracoff avatar Dec 28 '17 16:12 briankracoff

Same here, but also reproduced with your example code on 0.9.10 as well.

karpelcevs avatar Feb 20 '18 08:02 karpelcevs