Skip to content
This repository was archived by the owner on Jul 22, 2020. It is now read-only.

Commit d7cafd5

Browse files
nmartin867davidtruong
authored andcommitted
Add (void)clearSharedInstance to IterableAPI. (#37)
* Preserve orignal indentation in IterableAPI.m * Modify static initializers to check if sharedInstance is nil before creating new instance
1 parent 65b3997 commit d7cafd5

File tree

2 files changed

+28
-12
lines changed

2 files changed

+28
-12
lines changed

Iterable-iOS-SDK/IterableAPI.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,14 @@ typedef NS_ENUM(NSInteger, PushServicePlatform) {
173173
*/
174174
+ (nullable IterableAPI *)sharedInstance;
175175

176+
/*!
177+
@method
178+
179+
@abstract Sets the previously instantiated singleton instance of the API to nil
180+
181+
*/
182+
+ (void)clearSharedInstance;
183+
176184
/////////////////////////////
177185
/// @name Registering a token
178186
/////////////////////////////

Iterable-iOS-SDK/IterableAPI.m

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -381,26 +381,34 @@ + (IterableAPI *)sharedInstance
381381
return sharedInstance;
382382
}
383383

384+
// documented in IterableAPI.h
385+
+ (void)clearSharedInstance
386+
{
387+
@synchronized (self) {
388+
sharedInstance = nil;
389+
}
390+
}
391+
384392
// documented in IterableAPI.h
385393
+ (IterableAPI *)sharedInstanceWithApiKey:(NSString *)apiKey andEmail:(NSString *)email launchOptions:(NSDictionary *)launchOptions
386394
{
387-
// threadsafe way to create a static singleton https://stackoverflow.com/questions/5720029/create-singleton-using-gcds-dispatch-once-in-objective-c
388-
static dispatch_once_t onceToken;
389-
dispatch_once(&onceToken, ^{
390-
sharedInstance = [[IterableAPI alloc] initWithApiKey:apiKey andEmail:email launchOptions:launchOptions];
391-
});
392-
return sharedInstance;
395+
@synchronized (self) {
396+
if(!sharedInstance){
397+
sharedInstance = [[IterableAPI alloc] initWithApiKey:apiKey andEmail:email launchOptions:launchOptions];
398+
}
399+
return sharedInstance;
400+
}
393401
}
394402

395403
// documented in IterableAPI.h
396404
+ (IterableAPI *)sharedInstanceWithApiKey:(NSString *)apiKey andUserId:(NSString *)userId launchOptions:(NSDictionary *)launchOptions
397405
{
398-
// threadsafe way to create a static singleton https://stackoverflow.com/questions/5720029/create-singleton-using-gcds-dispatch-once-in-objective-c
399-
static dispatch_once_t onceToken;
400-
dispatch_once(&onceToken, ^{
401-
sharedInstance = [[IterableAPI alloc] initWithApiKey:apiKey andUserId:userId launchOptions:launchOptions];
402-
});
403-
return sharedInstance;
406+
@synchronized (self) {
407+
if(!sharedInstance){
408+
sharedInstance = [[IterableAPI alloc] initWithApiKey:apiKey andUserId:userId launchOptions:launchOptions];
409+
}
410+
return sharedInstance;
411+
}
404412
}
405413

406414
// documented in IterableAPI.h

0 commit comments

Comments
 (0)