Skip to content

Commit d546445

Browse files
author
Rodrigo Gomez Palacio
committed
fixup! OSMessagingController Retry Logic
1 parent eb06720 commit d546445

File tree

1 file changed

+26
-22
lines changed

1 file changed

+26
-22
lines changed

iOS_SDK/OneSignalSDK/OneSignalInAppMessages/Controller/OSMessagingController.m

Lines changed: 26 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -247,34 +247,37 @@ - (void)updateInAppMessagesFromCache {
247247
}
248248

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

252-
if (!subscriptionId) {
253-
[self updateInAppMessagesFromCache];
254-
return;
255-
}
253+
if (!subscriptionId) {
254+
[self updateInAppMessagesFromCache];
255+
return;
256+
}
256257

257-
OSConsistencyManager *consistencyManager = [OSConsistencyManager shared];
258-
NSString *onesignalId = OneSignalUserManagerImpl.sharedInstance.onesignalId;
258+
OSConsistencyManager *consistencyManager = [OSConsistencyManager shared];
259+
NSString *onesignalId = OneSignalUserManagerImpl.sharedInstance.onesignalId;
259260

260-
if (!onesignalId) {
261-
[OneSignalLog onesignalLog:ONE_S_LL_VERBOSE message:@"Failed to get in app messages due to no OneSignal ID"];
262-
return;
263-
}
261+
if (!onesignalId) {
262+
[OneSignalLog onesignalLog:ONE_S_LL_VERBOSE message:@"Failed to get in app messages due to no OneSignal ID"];
263+
return;
264+
}
264265

265-
OSIamFetchReadyCondition *condition = [OSIamFetchReadyCondition sharedInstanceWithId:onesignalId];
266-
NSString *rywToken = [consistencyManager registerCondition:condition forId:onesignalId];
266+
OSIamFetchReadyCondition *condition = [OSIamFetchReadyCondition sharedInstanceWithId:onesignalId];
267+
NSString *rywToken = [consistencyManager registerCondition:condition forId:onesignalId];
267268

268-
NSNumber *sessionDuration = @([OSSessionManager getTimeFocusedElapsed]);
269+
NSNumber *sessionDuration = @([OSSessionManager getTimeFocusedElapsed]);
269270

270-
// Initial request
271-
[self attemptFetchWithRetries:subscriptionId
272-
rywToken:rywToken
273-
sessionDuration:sessionDuration
274-
attempts:@0 // Starting with 0 attempts
275-
retryLimit:nil]; // Retry limit to be set dynamically on first failure
271+
// Initial request
272+
[self attemptFetchWithRetries:subscriptionId
273+
rywToken:rywToken
274+
sessionDuration:sessionDuration
275+
attempts:@0 // Starting with 0 attempts
276+
retryLimit:nil]; // Retry limit to be set dynamically on first failure
277+
});
276278
}
277279

280+
278281
- (void)attemptFetchWithRetries:(NSString *)subscriptionId
279282
rywToken:(NSString *)rywToken
280283
sessionDuration:(NSNumber *)sessionDuration
@@ -315,6 +318,7 @@ - (void)attemptFetchWithRetries:(NSString *)subscriptionId
315318
onFailure:^(NSError *error) {
316319
NSDictionary *errorInfo = error.userInfo[@"returned"];
317320
NSNumber *statusCode = errorInfo[@"httpStatusCode"];
321+
NSDictionary* responseHeaders = errorInfo[@"headers"];
318322

319323
if (!statusCode) {
320324
[self updateInAppMessagesFromCache];
@@ -325,11 +329,11 @@ - (void)attemptFetchWithRetries:(NSString *)subscriptionId
325329

326330
NSInteger code = [statusCode integerValue];
327331
if (code == 425 || code == 429) { // 425 Too Early or 429 Too Many Requests
328-
NSInteger retryAfter = [errorInfo[@"Retry-After"] integerValue] ?: DEFAULT_RETRY_AFTER_SECONDS;
332+
NSInteger retryAfter = [responseHeaders[@"Retry-After"] integerValue] ?: DEFAULT_RETRY_AFTER_SECONDS;
329333

330334
// Dynamically set the retry limit from the header, if not already set
331335
if (!blockRetryLimit) {
332-
blockRetryLimit = @([errorInfo[@"OneSignal-Retry-Limit"] integerValue] ?: DEFAULT_RETRY_LIMIT);
336+
blockRetryLimit = @([responseHeaders[@"OneSignal-Retry-Limit"] integerValue] ?: DEFAULT_RETRY_LIMIT);
333337
}
334338

335339
if ([attempts integerValue] < [blockRetryLimit integerValue]) {

0 commit comments

Comments
 (0)