From 23f729321d3e7068005f07518bc3112f60899276 Mon Sep 17 00:00:00 2001 From: Rodrigo Gomez Palacio Date: Fri, 18 Oct 2024 17:45:18 -0500 Subject: [PATCH] fixup! OSMessagingController Retry Logic --- .../Controller/OSMessagingController.m | 27 ++++++++++++------- 1 file changed, 17 insertions(+), 10 deletions(-) diff --git a/iOS_SDK/OneSignalSDK/OneSignalInAppMessages/Controller/OSMessagingController.m b/iOS_SDK/OneSignalSDK/OneSignalInAppMessages/Controller/OSMessagingController.m index 1baa78733..b3c7305bb 100644 --- a/iOS_SDK/OneSignalSDK/OneSignalInAppMessages/Controller/OSMessagingController.m +++ b/iOS_SDK/OneSignalSDK/OneSignalInAppMessages/Controller/OSMessagingController.m @@ -42,7 +42,7 @@ static NSInteger const DEFAULT_RETRY_AFTER_SECONDS = 1; // Default 1 second retry delay static NSInteger const DEFAULT_RETRY_LIMIT = 0; // If not returned by backend, don't retry -static NSInteger const IAM_FETCH_DELAY_BUFFER = 0.5; // Delay by 500 ms to increase the probability of getting a 200 & not having to retry +static NSInteger const IAM_FETCH_DELAY_BUFFER = 0.5; // Fallback value if ryw_delay is nil: delay by 500 ms to increase the probability of getting a 200 & not having to retry @implementation OSInAppMessageWillDisplayEvent @@ -265,14 +265,19 @@ - (void)getInAppMessagesFromServer:(NSString *)subscriptionId { } OSIamFetchReadyCondition *condition = [OSIamFetchReadyCondition sharedInstanceWithId:onesignalId]; - NSString *rywToken = [consistencyManager getRywTokenFromAwaitableCondition:condition forId:onesignalId]; - - // Delay by 500 ms to increase the probability of getting a 200 & not having to retry - dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(IAM_FETCH_DELAY_BUFFER * NSEC_PER_SEC)), dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ + OSReadYourWriteData *rywData = [consistencyManager getRywTokenFromAwaitableCondition:condition forId:onesignalId]; + + NSTimeInterval rywDelayInSeconds; + if (rywData.rywDelay) { + rywDelayInSeconds = [rywData.rywDelay doubleValue] / 1000.0; + } else { + rywDelayInSeconds = IAM_FETCH_DELAY_BUFFER; + } + dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(rywDelayInSeconds * NSEC_PER_SEC)), dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ // Initial request [self attemptFetchWithRetries:subscriptionId - rywToken:rywToken + rywData:rywData attempts:@0 // Starting with 0 attempts retryLimit:nil]; // Retry limit to be set dynamically on first failure }); @@ -281,10 +286,12 @@ - (void)getInAppMessagesFromServer:(NSString *)subscriptionId { - (void)attemptFetchWithRetries:(NSString *)subscriptionId - rywToken:(NSString *)rywToken + rywData:(OSReadYourWriteData *)rywData attempts:(NSNumber *)attempts retryLimit:(NSNumber *)retryLimit { NSNumber *sessionDuration = @([OSSessionManager.sharedSessionManager getTimeFocusedElapsed]); + NSString *rywToken = rywData.rywToken; + NSNumber *rywDelay = rywData.rywDelay; // Create the request with the current attempt count OSRequestGetInAppMessages *request = [OSRequestGetInAppMessages withSubscriptionId:subscriptionId @@ -338,7 +345,7 @@ - (void)attemptFetchWithRetries:(NSString *)subscriptionId NSInteger nextAttempt = [attempts integerValue] + 1; // Increment attempts [self retryAfterDelay:retryAfter subscriptionId:subscriptionId - rywToken:rywToken + rywData:rywData attempts:@(nextAttempt) retryLimit:blockRetryLimit]; } else { @@ -356,14 +363,14 @@ - (void)attemptFetchWithRetries:(NSString *)subscriptionId - (void)retryAfterDelay:(NSInteger)retryAfter subscriptionId:(NSString *)subscriptionId - rywToken:(NSString *)rywToken + rywData:(OSReadYourWriteData *)rywData attempts:(NSNumber *)attempts retryLimit:(NSNumber *)retryLimit { dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(retryAfter * NSEC_PER_SEC)), dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ [self attemptFetchWithRetries:subscriptionId - rywToken:rywToken + rywData:rywData attempts:attempts retryLimit:retryLimit]; });