Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: made reset and identify api's synchronous to reflect the data immediately #284

Merged
merged 15 commits into from Mar 27, 2023
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions Podfile.lock
Expand Up @@ -52,7 +52,7 @@ PODS:
- nanopb/encode (1.30906.0)
- PromisesObjC (1.2.12)
- Protobuf (3.21.12)
- Rudder (1.11.0)
- Rudder (1.12.0)

DEPENDENCIES:
- FirebaseMessaging
Expand Down Expand Up @@ -86,7 +86,7 @@ SPEC CHECKSUMS:
nanopb: 59317e09cf1f1a0af72f12af412d54edf52603fc
PromisesObjC: 3113f7f76903778cf4a0586bd1ab89329a0b7b97
Protobuf: 120350fc38646e2dedc26f49ecba778184ea1de2
Rudder: 9bb3913f997d2b248a7fd44c768994546c53411d
Rudder: 91e5ac5c13f2094da5440ff13cce4a7337a73c3b

PODFILE CHECKSUM: 70dfafab41ea667506f6d36e1e1a23f2c6e93495

Expand Down
4 changes: 4 additions & 0 deletions Rudder.xcodeproj/project.pbxproj
Expand Up @@ -199,6 +199,7 @@
ED8DB01E298E206900907EC4 /* RSConsentFilterHandler.m in Sources */ = {isa = PBXBuildFile; fileRef = ED8DB01D298E206900907EC4 /* RSConsentFilterHandler.m */; };
EDB3393629960F5E00706003 /* multi-dataresidency-eu-true.json in Resources */ = {isa = PBXBuildFile; fileRef = EDB3393429960F5E00706003 /* multi-dataresidency-eu-true.json */; };
EDB3393729960F5E00706003 /* multi-dataresidency-us-true.json in Resources */ = {isa = PBXBuildFile; fileRef = EDB3393529960F5E00706003 /* multi-dataresidency-us-true.json */; };
F64F9D7629BF171D009D963F /* ContextTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = F64F9D7529BF171D009D963F /* ContextTests.swift */; };
/* End PBXBuildFile section */

/* Begin PBXContainerItemProxy section */
Expand Down Expand Up @@ -419,6 +420,7 @@
EDB3393429960F5E00706003 /* multi-dataresidency-eu-true.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; path = "multi-dataresidency-eu-true.json"; sourceTree = "<group>"; };
EDB3393529960F5E00706003 /* multi-dataresidency-us-true.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; path = "multi-dataresidency-us-true.json"; sourceTree = "<group>"; };
EDEC3CDA29ADEF87007DDE07 /* github-release.config.js */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.javascript; path = "github-release.config.js"; sourceTree = "<group>"; };
F64F9D7529BF171D009D963F /* ContextTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ContextTests.swift; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand Down Expand Up @@ -479,6 +481,7 @@
ED7DFD80298C061A00ED5A8E /* DataResidencyTests.swift */,
ED04A23E29839DCC0080A88D /* EventRepositoryTests.swift */,
ED20F4F52996083B00F3FD0E /* TestUtils.swift */,
F64F9D7529BF171D009D963F /* ContextTests.swift */,
);
path = Tests;
sourceTree = "<group>";
Expand Down Expand Up @@ -1059,6 +1062,7 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
F64F9D7629BF171D009D963F /* ContextTests.swift in Sources */,
ED7DFD81298C061A00ED5A8E /* DataResidencyTests.swift in Sources */,
ED04A247298424040080A88D /* ConserFilterHandlerTests.swift in Sources */,
ED04A23F29839DCC0080A88D /* EventRepositoryTests.swift in Sources */,
Expand Down
14 changes: 7 additions & 7 deletions Sources/Classes/Public/RSContext.h
Expand Up @@ -44,21 +44,21 @@ extern int const RSATTAuthorize;
@property (nonatomic, readwrite) BOOL sessionStart;
@property (nonatomic, readwrite) NSMutableArray<NSMutableDictionary<NSString*, NSObject*>*> *externalIds;

+ (dispatch_queue_t)getQueue;
- (NSDictionary<NSString *, NSObject *>*)dict;
- (NSDictionary<NSString*, NSObject *>*)dict;
- (void)resetTraits;
- (void)updateTraits:(RSTraits *_Nullable) traits;
- (void)persistTraits;
- (void)updateTraits:(RSTraits* _Nullable)traits;
- (void)persistTraitsOnQueue;
- (void)updateTraitsDict:(NSMutableDictionary<NSString*, NSObject*>*)traitsDict;
- (void)updateTraitsAnonymousId;
- (void)putDeviceToken:(NSString*)deviceToken;
- (void)putAdvertisementId:(NSString *_Nonnull)idfa;
- (void)putAppTrackingConsent:(int)att;
- (void)updateExternalIds:(NSMutableArray *__nullable)externalIds;
- (void)resetExternalIds;
- (void)updateExternalIds:(NSMutableArray* __nullable)externalIds;
- (void)resetExternalIdsOnQueue;
- (void)persistExternalIds;
- (NSArray<NSDictionary<NSString*, NSObject*>*>* __nullable)getExternalIds;
- (void)setSessionData:(RSUserSession *)userSession;
- (void)setConsentData:(NSArray <NSString *> *)deniedConsentIds;
- (void)setConsentData:(NSArray <NSString*>*)deniedConsentIds;

@end

Expand Down
2 changes: 1 addition & 1 deletion Sources/Classes/Public/RSEventRepository.h
Expand Up @@ -45,7 +45,7 @@ NS_ASSUME_NONNULL_BEGIN
BOOL isSDKEnabled;
NSLock* lock;
dispatch_source_t source;
dispatch_queue_t queue;
dispatch_queue_t repositoryQueue;
RSClient *client;
}

Expand Down
4 changes: 3 additions & 1 deletion Sources/Classes/Public/RSOption.h
Expand Up @@ -11,7 +11,9 @@
NS_ASSUME_NONNULL_BEGIN

@protocol RSIntegrationFactory;
@interface RSOption : NSObject
@interface RSOption : NSObject {
dispatch_queue_t optionsQueue;
}

@property (nonatomic, strong) NSMutableArray<NSMutableDictionary<NSString*, NSObject*>*>* externalIds;
@property (nonatomic, strong) NSMutableDictionary<NSString*, NSObject*>* integrations;
Expand Down
2 changes: 2 additions & 0 deletions Sources/Classes/Public/RSPreferenceManager.h
Expand Up @@ -35,6 +35,7 @@ extern NSString *const RSSessionAutoTrackStatus;

- (void) saveTraits: (NSString* __nonnull) traits;
- (NSString* __nonnull) getTraits;
- (void) clearTraits;

- (void) saveBuildVersionCode:(NSString* __nonnull)versionCode;
- (NSString* __nullable) getBuildVersionCode;
Expand All @@ -54,6 +55,7 @@ extern NSString *const RSSessionAutoTrackStatus;

- (NSString* __nullable) getAnonymousId;
- (void) saveAnonymousId: (NSString* __nullable) anonymousId;
- (void) clearAnonymousId;

- (BOOL) getOptStatus;
- (void) saveOptStatus: (BOOL) optStatus;
Expand Down
116 changes: 62 additions & 54 deletions Sources/Classes/RSContext.m
Expand Up @@ -37,43 +37,36 @@ - (instancetype)init {
_timezone = [[NSTimeZone localTimeZone] name];
_externalIds = nil;

dispatch_sync(queue, ^{
NSString *traitsJson = [preferenceManager getTraits];
if (traitsJson == nil) {
// no persisted traits, create new and persist
[self createAndPersistTraits];

NSString *traitsJson = [preferenceManager getTraits];
if (traitsJson == nil) {
// no persisted traits, create new and persist
[self createAndPersistTraits];
} else {
NSError *error;
NSDictionary *traitsDict = [NSJSONSerialization JSONObjectWithData:[traitsJson dataUsingEncoding:NSUTF8StringEncoding] options:NSJSONReadingMutableContainers error:&error];
if (error == nil) {
_traits = [traitsDict mutableCopy];
} else {
NSError *error;
NSDictionary *traitsDict = [NSJSONSerialization JSONObjectWithData:[traitsJson dataUsingEncoding:NSUTF8StringEncoding] options:NSJSONReadingMutableContainers error:&error];
if (error == nil) {
_traits = [traitsDict mutableCopy];
} else {
// persisted traits persing error. initiate blank
[self createAndPersistTraits];
}
// persisted traits persing error. initiate blank
[self createAndPersistTraits];
}
// get saved external Ids from prefs
NSString *externalIdJson = [preferenceManager getExternalIds];
if (externalIdJson != nil) {
NSError *error;
NSDictionary *externalIdDict = [NSJSONSerialization JSONObjectWithData:[externalIdJson dataUsingEncoding:NSUTF8StringEncoding] options:NSJSONReadingMutableContainers error:&error];
if (error == nil) {
_externalIds = [externalIdDict mutableCopy];
}
}

// get saved external Ids from prefs
NSString *externalIdJson = [preferenceManager getExternalIds];
if (externalIdJson != nil) {
NSError *error;
NSDictionary *externalIdDict = [NSJSONSerialization JSONObjectWithData:[externalIdJson dataUsingEncoding:NSUTF8StringEncoding] options:NSJSONReadingMutableContainers error:&error];
if (error == nil) {
_externalIds = [externalIdDict mutableCopy];
}
});
}

}
return self;
}

+ (dispatch_queue_t) getQueue {
if (queue == nil) {
queue = dispatch_queue_create("com.rudder.RSContext", NULL);
}
return queue;
}

- (void) createAndPersistTraits {
RSTraits* traits = [[RSTraits alloc] init];
traits.anonymousId = [preferenceManager getAnonymousId];
Expand All @@ -83,60 +76,63 @@ - (void) createAndPersistTraits {
}

- (void) resetTraits {
dispatch_async(queue, ^{
dispatch_sync(queue, ^{
RSTraits* traits = [[RSTraits alloc] init];
traits.anonymousId = [self->preferenceManager getAnonymousId];
[self->_traits removeAllObjects];
[self->_traits setValuesForKeysWithDictionary:[traits dict]];
});
}

- (void)updateTraits:(RSTraits *)traits {
dispatch_async(queue, ^{
- (void) updateTraits:(RSTraits *)traits {
dispatch_sync(queue, ^{
NSString* existingId = (NSString*)[self->_traits objectForKey:@"userId"];
NSString* userId = (NSString*) traits.userId;

if(existingId!=nil && userId!=nil && ![existingId isEqual:userId])
{
self->_traits = [[traits dict]mutableCopy];
self->_traits = [[traits dict] mutableCopy];
[self resetExternalIds];
return;
}
[self->_traits setValuesForKeysWithDictionary:[traits dict]];
});
}
}

-(void) persistTraits {
dispatch_async(queue, ^{
NSData *traitsJsonData = [NSJSONSerialization dataWithJSONObject:[RSUtils serializeDict:self->_traits] options:0 error:nil];
NSString *traitsString = [[NSString alloc] initWithData:traitsJsonData encoding:NSUTF8StringEncoding];

[self->preferenceManager saveTraits:traitsString];
- (void) persistTraitsOnQueue {
dispatch_sync(queue, ^{
[self persistTraits];
});
}

-(void) persistTraits {
NSData *traitsJsonData = [NSJSONSerialization dataWithJSONObject:[RSUtils serializeDict:self->_traits] options:0 error:nil];
NSString *traitsString = [[NSString alloc] initWithData:traitsJsonData encoding:NSUTF8StringEncoding];
[self->preferenceManager saveTraits:traitsString];
}

- (void)updateTraitsDict:(NSMutableDictionary<NSString *, NSObject *> *)traitsDict {
dispatch_async(queue, ^{
dispatch_sync(queue, ^{
self->_traits = traitsDict;
});
}

- (void)updateTraitsAnonymousId {
dispatch_async(queue, ^{
dispatch_sync(queue, ^{
self->_traits[@"anonymousId"] = [self->preferenceManager getAnonymousId];
});
}

- (void)putDeviceToken:(NSString *)deviceToken {
dispatch_async(queue, ^{
dispatch_sync(queue, ^{
self->_device.token = deviceToken;
});
}

- (void)putAdvertisementId:(NSString *_Nonnull)idfa {
// This isn't ideal. We're doing this because we can't actually check if IDFA is enabled on
// the customer device. Apple docs and tests show that if it is disabled, one gets back all 0's.
dispatch_async(queue, ^{
dispatch_sync(queue, ^{
if( idfa != nil && [idfa length] != 0) {
[RSLogger logDebug:[[NSString alloc] initWithFormat:@"IDFA: %@", idfa]];
BOOL adTrackingEnabled = (![idfa isEqualToString:@"00000000-0000-0000-0000-000000000000"]);
Expand All @@ -150,7 +146,7 @@ - (void)putAdvertisementId:(NSString *_Nonnull)idfa {
}

- (void)updateExternalIds:(NSMutableArray *)externalIds {
dispatch_async(queue, ^{
dispatch_sync(queue, ^{
if(self->_externalIds == nil)
{
self->_externalIds = [[NSMutableArray alloc] init];
Expand Down Expand Up @@ -178,7 +174,7 @@ - (void)updateExternalIds:(NSMutableArray *)externalIds {
}

- (void)persistExternalIds {
dispatch_async(queue, ^{
dispatch_sync(queue, ^{
if (self->_externalIds != nil) {
// update persistence storage
NSData *externalIdJsonData = [NSJSONSerialization dataWithJSONObject:[RSUtils serializeArray:[self->_externalIds copy]] options:0 error:nil];
Expand All @@ -188,15 +184,27 @@ - (void)persistExternalIds {
});
}

- (void)resetExternalIds {
dispatch_async(queue, ^{
self->_externalIds = nil;
[self->preferenceManager clearExternalIds];
- (NSArray<NSDictionary<NSString*, NSObject*>*>* __nullable) getExternalIds {
__block NSArray<NSDictionary<NSString*, NSObject*>*>* externalIdsCopy = nil;
dispatch_sync(queue, ^{
externalIdsCopy = [self->_externalIds copy];
});
return externalIdsCopy;
}

- (void) resetExternalIdsOnQueue {
dispatch_sync(queue, ^{
[self resetExternalIds];
});
}

- (void)resetExternalIds {
self->_externalIds = nil;
[self->preferenceManager clearExternalIds];
}

- (void)putAppTrackingConsent:(int)att {
dispatch_async(queue, ^{
dispatch_sync(queue, ^{
if (att < RSATTNotDetermined) {
self->_device.attTrackingStatus = RSATTNotDetermined;
} else if (att > RSATTAuthorize) {
Expand All @@ -208,7 +216,7 @@ - (void)putAppTrackingConsent:(int)att {
}

- (void) setSessionData:(RSUserSession *) userSession {
dispatch_async(queue, ^{
dispatch_sync(queue, ^{
if ([userSession getSessionId] != nil) {
self->_sessionId = [userSession getSessionId];
if([userSession getSessionStart]) {
Expand All @@ -220,7 +228,7 @@ - (void) setSessionData:(RSUserSession *) userSession {
}

- (void)setConsentData:(NSArray <NSString *> *)deniedConsentIds {
dispatch_async(queue, ^{
dispatch_sync(queue, ^{
self->consentManagement = @{@"deniedConsentIds": deniedConsentIds};
});
}
Expand Down
37 changes: 14 additions & 23 deletions Sources/Classes/RSElementCache.m
Expand Up @@ -23,46 +23,37 @@ + (RSContext *)getContext {
}

+ (void)updateTraits:(RSTraits *)traits {
dispatch_async([RSContext getQueue], ^{
[cachedContext updateTraits:traits];
[self persistTraits];
});
[cachedContext updateTraits:traits];
[self persistTraits];
}


+ (void)persistTraits {
[cachedContext persistTraits];
[cachedContext persistTraitsOnQueue];
}

+ (void) reset {
dispatch_async([RSContext getQueue], ^{
[cachedContext resetTraits];
[cachedContext persistTraits];
[cachedContext resetExternalIds];
});
[cachedContext resetTraits];
[cachedContext persistTraitsOnQueue];
[cachedContext resetExternalIdsOnQueue];
}

+ (void)updateTraitsDict:(NSMutableDictionary<NSString *,NSObject *> *)traitsDict {
dispatch_async([RSContext getQueue], ^{
[cachedContext updateTraitsDict: traitsDict];
[self persistTraits];
});
[cachedContext updateTraitsDict: traitsDict];
[self persistTraits];
}

+(void) updateTraitsAnonymousId {
dispatch_async([RSContext getQueue], ^{
[cachedContext updateTraitsAnonymousId];
[self persistTraits];
});
+ (void) updateTraitsAnonymousId {
[cachedContext updateTraitsAnonymousId];
[self persistTraits];
}

+ (NSString *)getAnonymousId {
return [[RSPreferenceManager getInstance] getAnonymousId];
}

+ (void) updateExternalIds:(NSMutableArray *)externalIds {
dispatch_async([RSContext getQueue], ^{
[cachedContext updateExternalIds:externalIds];
[cachedContext persistExternalIds];
});
[cachedContext updateExternalIds:externalIds];
[cachedContext persistExternalIds];
}
@end