Skip to content

Commit c299bae

Browse files
authored
#86 Fix singletone class initialisation (#87)
1 parent 54356e0 commit c299bae

File tree

1 file changed

+16
-9
lines changed

1 file changed

+16
-9
lines changed

ios/RNVoipPushNotification/RNVoipPushNotificationManager.m

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,27 +30,34 @@ @implementation RNVoipPushNotificationManager
3030
static NSString *_lastVoipToken = @"";
3131
static NSMutableDictionary<NSString *, RNVoipPushNotificationCompletion> *completionHandlers = nil;
3232

33-
3433
// =====
3534
// ===== RN Module Configure and Override =====
3635
// =====
3736

37+
-(instancetype) init
38+
{
39+
return [[self class] sharedInstance];
40+
}
3841

39-
- (instancetype)init
42+
-(instancetype) initPrivate
4043
{
4144
if (self = [super init]) {
4245
_delayedEvents = [NSMutableArray array];
4346
}
47+
4448
return self;
4549
}
4650

47-
+ (id)allocWithZone:(NSZone *)zone {
48-
static RNVoipPushNotificationManager *sharedInstance = nil;
51+
// Singletone implementation based on https://stackoverflow.com/q/5720029/3686678 and https://stackoverflow.com/a/7035136/3686678
52+
+(instancetype) sharedInstance
53+
{
54+
static RNVoipPushNotificationManager *sharedVoipPushManager = nil;
4955
static dispatch_once_t onceToken;
5056
dispatch_once(&onceToken, ^{
51-
sharedInstance = [super allocWithZone:zone];
57+
sharedVoipPushManager = [[self alloc] initPrivate];
5258
});
53-
return sharedInstance;
59+
60+
return sharedVoipPushManager;
5461
}
5562

5663
// --- clean observer and completionHandlers when app close
@@ -126,7 +133,7 @@ + (void)voipRegistration
126133
#ifdef DEBUG
127134
RCTLog(@"[RNVoipPushNotificationManager] voipRegistration is already registered. return _lastVoipToken = %@", _lastVoipToken);
128135
#endif
129-
RNVoipPushNotificationManager *voipPushManager = [RNVoipPushNotificationManager allocWithZone: nil];
136+
RNVoipPushNotificationManager *voipPushManager = [RNVoipPushNotificationManager sharedInstance];
130137
[voipPushManager sendEventWithNameWrapper:RNVoipPushRemoteNotificationsRegisteredEvent body:_lastVoipToken];
131138
} else {
132139
_isVoipRegistered = YES;
@@ -164,7 +171,7 @@ + (void)didUpdatePushCredentials:(PKPushCredentials *)credentials forType:(NSStr
164171

165172
_lastVoipToken = [hexString copy];
166173

167-
RNVoipPushNotificationManager *voipPushManager = [RNVoipPushNotificationManager allocWithZone: nil];
174+
RNVoipPushNotificationManager *voipPushManager = [RNVoipPushNotificationManager sharedInstance];
168175
[voipPushManager sendEventWithNameWrapper:RNVoipPushRemoteNotificationsRegisteredEvent body:_lastVoipToken];
169176
}
170177

@@ -175,7 +182,7 @@ + (void)didReceiveIncomingPushWithPayload:(PKPushPayload *)payload forType:(NSSt
175182
RCTLog(@"[RNVoipPushNotificationManager] didReceiveIncomingPushWithPayload payload.dictionaryPayload = %@, type = %@", payload.dictionaryPayload, type);
176183
#endif
177184

178-
RNVoipPushNotificationManager *voipPushManager = [RNVoipPushNotificationManager allocWithZone: nil];
185+
RNVoipPushNotificationManager *voipPushManager = [RNVoipPushNotificationManager sharedInstance];
179186
[voipPushManager sendEventWithNameWrapper:RNVoipPushRemoteNotificationReceivedEvent body:payload.dictionaryPayload];
180187
}
181188

0 commit comments

Comments
 (0)