marmelroy / PhoneNumberKit

A Swift framework for parsing, formatting and validating international phone numbers. Inspired by Google's libphonenumber.
MIT License
5.1k stars 810 forks source link

Question: Is it safe to initialize PhoneNumberKit on a background thread? #788

Closed justinHowlett closed 1 month ago

justinHowlett commented 1 month ago

Hello, since PhoneNumberKit parses a large amount of metadata JSON on initialization I would like to know whether it is currently safe and will continue to be safe to initialize PhoneNumberKit on a background thread.

i.e is PhoneNumberKit thread safe / same-thread safe etc...

Currently the initialization causes a 33-50ms stall on my iPhone 12 Pro.

Thanks.

bguidolim commented 1 month ago

Hey @justinHowlett

I don't see any reason why it wouldn't be safe. Have you faced any issue with that?

AFAIK, the metadata is loaded every time a new instance is created, the reason is probably because PNK is not something that you would keep it active for a long time, but if you do, then you are responsible of keeping the instance alive.

There were some issues regarding thread safety in the past, but it shouldn't be the case in the current state.

I hope it helps you.

justinHowlett commented 1 month ago

Thanks @bguidolim it looks thread-safe yes. I just didn't want to assume it would be thread-safe moving forward. Having a future version break the thread safety guarantee for instance.

I appreciate your quick response!