Skip to content

Commit

Permalink
incorporate lastTokenElapsed into minInterval calculation
Browse files Browse the repository at this point in the history
  • Loading branch information
nickpatrick committed Jan 13, 2025
1 parent aeb037d commit f189548
Showing 1 changed file with 11 additions and 7 deletions.
18 changes: 11 additions & 7 deletions RadarSDK/RadarVerificationManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -203,16 +203,20 @@ - (void)intervalFired {
[self callTrackVerified];
}

- (void)scheduleNextIntervalWithToken:(RadarVerifiedLocationToken *)token {
- (void)scheduleNextIntervalWithLastToken {
NSTimeInterval expiresIn = 0;
NSTimeInterval minInterval = self.startedInterval;

if (token) {
expiresIn = token.expiresIn;
if (self.lastToken) {
expiresIn = self.lastToken.expiresIn;

NSTimeInterval lastTokenElapsed = [NSProcessInfo processInfo].systemUptime - self.lastTokenSystemUptime;

// if expiresIn is shorter than interval, override interval
// re-request early to maximize the likelihood that a cached token is available
minInterval = MIN(expiresIn - 10, self.startedInterval);
minInterval = MIN(expiresIn - 10 - lastTokenElapsed, self.startedInterval);

[[RadarLogger sharedInstance] logWithLevel:RadarLogLevelDebug message:[NSString stringWithFormat:@"Calculated next interval | minInterval = %f; expiresIn = %f; lastTokenElapsed = %f, startedInterval = %f", minInterval, expiresIn, lastTokenElapsed, self.startedInterval]];
}

// min interval is 10 seconds
Expand All @@ -226,7 +230,7 @@ - (void)scheduleNextIntervalWithToken:(RadarVerifiedLocationToken *)token {
return;
}

[[RadarLogger sharedInstance] logWithLevel:RadarLogLevelDebug message:[NSString stringWithFormat:@"Requesting token again in %f seconds | minInterval = %f; expiresIn = %f; startedInterval = %f", minInterval, minInterval, expiresIn, self.startedInterval]];
[[RadarLogger sharedInstance] logWithLevel:RadarLogLevelDebug message:[NSString stringWithFormat:@"Requesting token again in %f seconds", minInterval]];

[self performSelector:@selector(intervalFired) withObject:nil afterDelay:minInterval];
}
Expand All @@ -237,7 +241,7 @@ - (void)callTrackVerified {
}

[self trackVerifiedWithBeacons:self.startedBeacons desiredAccuracy:RadarTrackingOptionsDesiredAccuracyHigh completionHandler:^(RadarStatus status, RadarVerifiedLocationToken *_Nullable token) {
[self scheduleNextIntervalWithToken:token];
[self scheduleNextIntervalWithLastToken];
}];
}

Expand Down Expand Up @@ -285,7 +289,7 @@ - (void)startTrackingVerifiedWithInterval:(NSTimeInterval)interval beacons:(BOOL
}

if ([self isLastTokenValid]) {
[self scheduleNextIntervalWithToken:self.lastToken];
[self scheduleNextIntervalWithLastToken];
} else {
[self callTrackVerified];
}
Expand Down

0 comments on commit f189548

Please sign in to comment.