Skip to content

Commit

Permalink
#86 Fix singletone class initialisation (#87)
Browse files Browse the repository at this point in the history
  • Loading branch information
Romick2005 committed May 19, 2021
1 parent 54356e0 commit c299bae
Showing 1 changed file with 16 additions and 9 deletions.
25 changes: 16 additions & 9 deletions ios/RNVoipPushNotification/RNVoipPushNotificationManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -30,27 +30,34 @@ @implementation RNVoipPushNotificationManager
static NSString *_lastVoipToken = @"";
static NSMutableDictionary<NSString *, RNVoipPushNotificationCompletion> *completionHandlers = nil;


// =====
// ===== RN Module Configure and Override =====
// =====

-(instancetype) init
{
return [[self class] sharedInstance];
}

- (instancetype)init
-(instancetype) initPrivate
{
if (self = [super init]) {
_delayedEvents = [NSMutableArray array];
}

return self;
}

+ (id)allocWithZone:(NSZone *)zone {
static RNVoipPushNotificationManager *sharedInstance = nil;
// Singletone implementation based on https://stackoverflow.com/q/5720029/3686678 and https://stackoverflow.com/a/7035136/3686678
+(instancetype) sharedInstance
{
static RNVoipPushNotificationManager *sharedVoipPushManager = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
sharedInstance = [super allocWithZone:zone];
sharedVoipPushManager = [[self alloc] initPrivate];
});
return sharedInstance;

return sharedVoipPushManager;
}

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

_lastVoipToken = [hexString copy];

RNVoipPushNotificationManager *voipPushManager = [RNVoipPushNotificationManager allocWithZone: nil];
RNVoipPushNotificationManager *voipPushManager = [RNVoipPushNotificationManager sharedInstance];
[voipPushManager sendEventWithNameWrapper:RNVoipPushRemoteNotificationsRegisteredEvent body:_lastVoipToken];
}

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

RNVoipPushNotificationManager *voipPushManager = [RNVoipPushNotificationManager allocWithZone: nil];
RNVoipPushNotificationManager *voipPushManager = [RNVoipPushNotificationManager sharedInstance];
[voipPushManager sendEventWithNameWrapper:RNVoipPushRemoteNotificationReceivedEvent body:payload.dictionaryPayload];
}

Expand Down

0 comments on commit c299bae

Please sign in to comment.