Skip to content

Commit

Permalink
Remove IAM Caching
Browse files Browse the repository at this point in the history
Motivation: we should not cache IAMs since it is incongruent with RYW consistency.

Example: there is an IAM targeting users on version 1.0. The user upgrades to 2.0 & should no longer see the IAM targeting 1.0.

The source of truth is the backend & we should only show IAMs as calculated by segment membership.
  • Loading branch information
rgomezp committed Oct 15, 2024
1 parent 10e877b commit 1068348
Show file tree
Hide file tree
Showing 5 changed files with 1 addition and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -77,15 +77,6 @@ + (void)migrateToOSInAppMessageInternal {
[OneSignalLog onesignalLog:ONE_S_LL_DEBUG message:[NSString stringWithFormat:@"Migrating OSInAppMessage from version: %ld", sdkVersion]];

[NSKeyedUnarchiver setClass:[OSInAppMessageInternal class] forClassName:@"OSInAppMessage"];
// Messages Array
NSArray<OSInAppMessageInternal *> *messages = [OneSignalUserDefaults.initStandard getSavedCodeableDataForKey:OS_IAM_MESSAGES_ARRAY
defaultValue:[NSArray<OSInAppMessageInternal *> new]];
if (messages && messages.count) {
[NSKeyedArchiver setClassName:@"OSInAppMessageInternal" forClass:[OSInAppMessageInternal class]];
[OneSignalUserDefaults.initStandard saveCodeableDataForKey:OS_IAM_MESSAGES_ARRAY withValue:messages];
} else {
[OneSignalUserDefaults.initStandard saveCodeableDataForKey:OS_IAM_MESSAGES_ARRAY withValue:nil];
}

// Redisplay Messages Dict
NSMutableDictionary <NSString *, OSInAppMessageInternal *> *redisplayedInAppMessages = [[NSMutableDictionary alloc] initWithDictionary:[OneSignalUserDefaults.initStandard getSavedCodeableDataForKey:OS_IAM_REDISPLAY_DICTIONARY defaultValue:[NSMutableDictionary new]]];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ NS_ASSUME_NONNULL_BEGIN
+ (void)start;
+ (void)removeInstance;
- (void)presentInAppMessage:(OSInAppMessageInternal *)message;
- (void)updateInAppMessagesFromCache;
- (void)getInAppMessagesFromServer:(NSString * _Nullable)subscriptionId;
- (void)messageViewImpressionRequest:(OSInAppMessageInternal *)message;
- (void)messageViewPageImpressionRequest:(OSInAppMessageInternal *)message withPageId:(NSString *)pageId;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -206,8 +206,7 @@ - (instancetype)init {
self.dateGenerator = ^ NSTimeInterval {
return [[NSDate date] timeIntervalSince1970];
};
self.messages = [OneSignalUserDefaults.initStandard getSavedCodeableDataForKey:OS_IAM_MESSAGES_ARRAY
defaultValue:[NSArray<OSInAppMessageInternal *> new]];
self.messages = [NSArray<OSInAppMessageInternal *> new];
[self initializeTriggerController];
self.messageDisplayQueue = [NSMutableArray new];
self.clickListeners = [NSMutableArray new];
Expand Down Expand Up @@ -241,17 +240,11 @@ - (void)initializeTriggerController {
dateFromString:timeSinceLastMessage]];
}

- (void)updateInAppMessagesFromCache {
self.messages = [OneSignalUserDefaults.initStandard getSavedCodeableDataForKey:OS_IAM_MESSAGES_ARRAY defaultValue:[NSArray new]];
[self evaluateMessages];
}

- (void)getInAppMessagesFromServer:(NSString *)subscriptionId {
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
[OneSignalLog onesignalLog:ONE_S_LL_VERBOSE message:@"getInAppMessagesFromServer"];

if (!subscriptionId) {
[self updateInAppMessagesFromCache];
return;
}

Expand Down Expand Up @@ -314,7 +307,6 @@ - (void)attemptFetchWithRetries:(NSString *)subscriptionId
NSDictionary* responseHeaders = errorInfo[@"headers"];

if (!statusCode) {
[self updateInAppMessagesFromCache];
return;
}

Expand Down Expand Up @@ -342,9 +334,6 @@ - (void)attemptFetchWithRetries:(NSString *)subscriptionId
}
} else if (code >= 500 && code <= 599) {
[OneSignalLog onesignalLog:ONE_S_LL_VERBOSE message:@"Server error, skipping retries"];
[self updateInAppMessagesFromCache];
} else {
[self updateInAppMessagesFromCache];
}
}];
}
Expand Down Expand Up @@ -389,23 +378,15 @@ - (void)fetchInAppMessagesWithoutToken:(NSString *)subscriptionId {
[self updateInAppMessagesFromServer:messages];
return;
}
[self updateInAppMessagesFromCache];
});
} onFailure:^(NSError *error) {
[OneSignalLog onesignalLog:ONE_S_LL_VERBOSE message:[NSString stringWithFormat:@"getInAppMessagesFromServer failure: %@", error.localizedDescription]];
[self updateInAppMessagesFromCache];
}];
}

- (void)updateInAppMessagesFromServer:(NSArray<OSInAppMessageInternal *> *)newMessages {
[OneSignalLog onesignalLog:ONE_S_LL_VERBOSE message:@"updateInAppMessagesFromServer"];
self.messages = newMessages;

// Cache if messages passed in are not null, this method is called from on_session for
// new messages and cached when foregrounding app
if (self.messages)
[OneSignalUserDefaults.initStandard saveCodeableDataForKey:OS_IAM_MESSAGES_ARRAY withValue:self.messages];

self.calledLoadTags = NO;
[self resetRedisplayMessagesBySession];
[self evaluateMessages];
Expand All @@ -426,11 +407,6 @@ - (void)deleteInactiveMessage:(OSInAppMessageInternal *)message {
NSMutableArray *newMessagesArray = [NSMutableArray arrayWithArray:self.messages];
[newMessagesArray removeObject: message];
self.messages = newMessagesArray;
if (self.messages) {
[OneSignalUserDefaults.initStandard saveCodeableDataForKey:OS_IAM_MESSAGES_ARRAY withValue:self.messages];
} else {
[OneSignalUserDefaults.initStandard removeValueForKey:OS_IAM_MESSAGES_ARRAY];
}
}

/*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ typedef NS_ENUM(NSUInteger, OSTriggerOperatorType) {
#define OS_IAM_CLICKED_SET_KEY @"OS_IAM_CLICKED_SET"
#define OS_IAM_IMPRESSIONED_SET_KEY @"OS_IAM_IMPRESSIONED_SET"
#define OS_IAM_PAGE_IMPRESSIONED_SET_KEY @"OS_IAM_PAGE_IMPRESSIONED_SET"
#define OS_IAM_MESSAGES_ARRAY @"OS_IAM_MESSAGES_ARRAY"
#define OS_IAM_REDISPLAY_DICTIONARY @"OS_IAM_REDISPLAY_DICTIONARY"
#define OS_IAM_TIME_SINCE_LAST_MESSAGE_KEY @"OS_IAM_TIME_SINCE_LAST_MESSAGE"

Expand Down
3 changes: 0 additions & 3 deletions iOS_SDK/OneSignalSDK/Source/OneSignal.m
Original file line number Diff line number Diff line change
Expand Up @@ -417,9 +417,6 @@ + (void)startNewSessionInternal {
// [OneSignalLocation sendLocation];
// [self executePendingLiveActivityUpdates];
// [self receivedInAppMessageJson:results[@"push"][@"in_app_messages"]]; // go to controller

// on failure:
// [OSMessagingController.sharedInstance updateInAppMessagesFromCache]; // go to controller
}

+ (void)startInAppMessages {
Expand Down

0 comments on commit 1068348

Please sign in to comment.