From 2bf05f1ef3c1a3638e2ef31d6733f84d73075db4 Mon Sep 17 00:00:00 2001 From: Gene Goldfarb Date: Fri, 27 Jan 2023 01:37:08 -0800 Subject: [PATCH 1/2] Switch NBShortNumberUtil to a singleton pattern. This mirrors NBPhoneNumberUtil API and prevents unnecessary resource usage due to loading of metadata. --- libPhoneNumberShortNumber/NBShortNumberUtil.h | 6 ++++-- libPhoneNumberShortNumber/NBShortNumberUtil.m | 16 +++++++++++----- 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/libPhoneNumberShortNumber/NBShortNumberUtil.h b/libPhoneNumberShortNumber/NBShortNumberUtil.h index 121c6a7a..8208b3db 100644 --- a/libPhoneNumberShortNumber/NBShortNumberUtil.h +++ b/libPhoneNumberShortNumber/NBShortNumberUtil.h @@ -25,8 +25,10 @@ typedef NS_ENUM(NSUInteger, NBEShortNumberCost) { @property(nonatomic) NSDictionary *> *countryToRegionCodeMap; -/// Initialize short number util with a default metadata helper. -- (instancetype)init; +/// Short number util singleton with a default metadata helper. ++ (NBShortNumberUtil *)sharedInstance; + +- (instancetype)init NS_UNAVAILABLE; /// Initialize short number util with a metadata helper. /// @param helper A metadata helper. diff --git a/libPhoneNumberShortNumber/NBShortNumberUtil.m b/libPhoneNumberShortNumber/NBShortNumberUtil.m index 270ab5b2..33986a01 100644 --- a/libPhoneNumberShortNumber/NBShortNumberUtil.m +++ b/libPhoneNumberShortNumber/NBShortNumberUtil.m @@ -25,6 +25,17 @@ @implementation NBShortNumberUtil { NBPhoneNumberUtil *_phoneUtil; } ++ (NBShortNumberUtil *)sharedInstance { + static NBShortNumberUtil *sharedOnceInstance = nil; + static dispatch_once_t onceToken; + dispatch_once(&onceToken, ^{ + sharedOnceInstance = + [[self alloc] initWithMetadataHelper:[[NBShortNumberMetadataHelper alloc] init] + phoneNumberUtil:[NBPhoneNumberUtil sharedInstance]]; + }); + return sharedOnceInstance; +} + - (instancetype)initWithMetadataHelper:(NBShortNumberMetadataHelper *)helper phoneNumberUtil:(NBPhoneNumberUtil *)phoneNumberUtil { self = [super init]; @@ -36,11 +47,6 @@ - (instancetype)initWithMetadataHelper:(NBShortNumberMetadataHelper *)helper return self; } -- (instancetype)init { - return [self initWithMetadataHelper:[[NBShortNumberMetadataHelper alloc] init] - phoneNumberUtil:[NBPhoneNumberUtil sharedInstance]]; -} - - (BOOL)isPossibleShortNumber:(NBPhoneNumber *)phoneNumber forRegion:(NSString *)regionDialingFrom { if (![self doesPhoneNumber:phoneNumber matchesRegion:regionDialingFrom]) { return NO; From a1ae914a87bfe71066b6f81d0caff6f490bf288f Mon Sep 17 00:00:00 2001 From: Gene Goldfarb Date: Fri, 27 Jan 2023 01:37:08 -0800 Subject: [PATCH 2/2] Switch NBShortNumberUtil to a singleton pattern. This mirrors NBPhoneNumberUtil API and prevents unnecessary resource usage due to loading of metadata. --- libPhoneNumberShortNumber/NBShortNumberUtil.h | 6 ++++-- libPhoneNumberShortNumber/NBShortNumberUtil.m | 16 +++++++++++----- libPhoneNumberShortNumber/README.md | 2 +- 3 files changed, 16 insertions(+), 8 deletions(-) diff --git a/libPhoneNumberShortNumber/NBShortNumberUtil.h b/libPhoneNumberShortNumber/NBShortNumberUtil.h index 121c6a7a..8208b3db 100644 --- a/libPhoneNumberShortNumber/NBShortNumberUtil.h +++ b/libPhoneNumberShortNumber/NBShortNumberUtil.h @@ -25,8 +25,10 @@ typedef NS_ENUM(NSUInteger, NBEShortNumberCost) { @property(nonatomic) NSDictionary *> *countryToRegionCodeMap; -/// Initialize short number util with a default metadata helper. -- (instancetype)init; +/// Short number util singleton with a default metadata helper. ++ (NBShortNumberUtil *)sharedInstance; + +- (instancetype)init NS_UNAVAILABLE; /// Initialize short number util with a metadata helper. /// @param helper A metadata helper. diff --git a/libPhoneNumberShortNumber/NBShortNumberUtil.m b/libPhoneNumberShortNumber/NBShortNumberUtil.m index 270ab5b2..33986a01 100644 --- a/libPhoneNumberShortNumber/NBShortNumberUtil.m +++ b/libPhoneNumberShortNumber/NBShortNumberUtil.m @@ -25,6 +25,17 @@ @implementation NBShortNumberUtil { NBPhoneNumberUtil *_phoneUtil; } ++ (NBShortNumberUtil *)sharedInstance { + static NBShortNumberUtil *sharedOnceInstance = nil; + static dispatch_once_t onceToken; + dispatch_once(&onceToken, ^{ + sharedOnceInstance = + [[self alloc] initWithMetadataHelper:[[NBShortNumberMetadataHelper alloc] init] + phoneNumberUtil:[NBPhoneNumberUtil sharedInstance]]; + }); + return sharedOnceInstance; +} + - (instancetype)initWithMetadataHelper:(NBShortNumberMetadataHelper *)helper phoneNumberUtil:(NBPhoneNumberUtil *)phoneNumberUtil { self = [super init]; @@ -36,11 +47,6 @@ - (instancetype)initWithMetadataHelper:(NBShortNumberMetadataHelper *)helper return self; } -- (instancetype)init { - return [self initWithMetadataHelper:[[NBShortNumberMetadataHelper alloc] init] - phoneNumberUtil:[NBPhoneNumberUtil sharedInstance]]; -} - - (BOOL)isPossibleShortNumber:(NBPhoneNumber *)phoneNumber forRegion:(NSString *)regionDialingFrom { if (![self doesPhoneNumber:phoneNumber matchesRegion:regionDialingFrom]) { return NO; diff --git a/libPhoneNumberShortNumber/README.md b/libPhoneNumberShortNumber/README.md index 01ee9344..39308a1a 100644 --- a/libPhoneNumberShortNumber/README.md +++ b/libPhoneNumberShortNumber/README.md @@ -46,7 +46,7 @@ import libPhoneNumberShortNumber ## Usage - **NBShortNumberUtil** ```obj-c -NBShortNumberUtil *shortNumberUtil = [[NBShortNumberUtil alloc] init]; +NBShortNumberUtil *shortNumberUtil = [NBShortNumberUtil sharedInstance]; // possibleNumber : +33123456 NSLog(@"Is possible short number: %d",