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

fix: set _inForeground value earlier #433

Merged
merged 8 commits into from
May 26, 2023
32 changes: 15 additions & 17 deletions Sources/Amplitude/Amplitude.m
Original file line number Diff line number Diff line change
@@ -482,6 +482,8 @@ - (void) checkInForeground {
// Normally _inForeground is set by the enterForeground callback, but initializeWithApiKey will be called after the app's enterForeground
// notification is already triggered, so we need to manually check and set it now.
// UIApplication methods are only allowed on the main thread so need to dispatch this synchronously to the main thread.
self->_inForeground = YES;
justin-fiedler marked this conversation as resolved.
Show resolved Hide resolved

void (^checkInForeground)(void) = ^{
#if !TARGET_OS_OSX && !TARGET_OS_WATCH
UIApplication *app = [AMPUtils getSharedApplication];
@@ -494,8 +496,7 @@ - (void) checkInForeground {
[self refreshDynamicConfig];

NSNumber *now = [NSNumber numberWithLongLong:[[self currentTime] timeIntervalSince1970] * 1000];
[self startOrContinueSessionNSNumber:now];
self->_inForeground = YES;
[self startOrContinueSessionNSNumber:now inForeground:NO];
justin-fiedler marked this conversation as resolved.
Show resolved Hide resolved
#if !TARGET_OS_OSX && !TARGET_OS_WATCH
}];

@@ -603,6 +604,7 @@ - (void)logEvent:(NSString *)eventType withEventProperties:(NSDictionary *)event
userProperties = [userProperties copy];
groups = [groups copy];
groupProperties = [groupProperties copy];
BOOL inForeground = _inForeground;

[self runOnBackgroundQueue:^{
// Respect the opt-out setting by not sending or storing any events.
@@ -614,7 +616,7 @@ - (void)logEvent:(NSString *)eventType withEventProperties:(NSDictionary *)event
// skip session check if logging start_session or end_session events
BOOL loggingSessionEvent = self->_trackingSessionEvents && ([eventType isEqualToString:kAMPSessionStartEvent] || [eventType isEqualToString:kAMPSessionEndEvent]);
if (!loggingSessionEvent && !outOfSession) {
[self startOrContinueSessionNSNumber:timestamp];
[self startOrContinueSessionNSNumber:timestamp inForeground:inForeground];
}

NSMutableDictionary *event = [NSMutableDictionary dictionary];
@@ -1101,40 +1103,37 @@ - (void)makeEventUploadPostRequest:(NSString *)url events:(NSString *)events num
#pragma mark - application lifecycle methods

- (void)enterForeground {
self->_inForeground = YES;
NSNumber *now = [NSNumber numberWithLongLong:[[self currentTime] timeIntervalSince1970] * 1000];

#if !TARGET_OS_OSX && !TARGET_OS_WATCH
UIApplication *app = [AMPUtils getSharedApplication];
if (app == nil) {
return;
}
#endif

NSNumber *now = [NSNumber numberWithLongLong:[[self currentTime] timeIntervalSince1970] * 1000];

#if !TARGET_OS_OSX && !TARGET_OS_WATCH
// Stop uploading
[self endBackgroundTaskIfNeeded];
#endif
[self runOnBackgroundQueue:^{
// Fetch the data ingestion endpoint based on current device's geo location.

[self refreshDynamicConfig];
[self startOrContinueSessionNSNumber:now];
self->_inForeground = YES;
[self startOrContinueSessionNSNumber:now inForeground:NO];
[self uploadEvents];
}];
}

- (void)enterBackground {
self->_inForeground = NO;
NSNumber *now = [NSNumber numberWithLongLong:[[self currentTime] timeIntervalSince1970] * 1000];

#if !TARGET_OS_OSX && !TARGET_OS_WATCH
UIApplication *app = [AMPUtils getSharedApplication];
if (app == nil) {
return;
}
#endif

NSNumber *now = [NSNumber numberWithLongLong:[[self currentTime] timeIntervalSince1970] * 1000];

#if !TARGET_OS_OSX && !TARGET_OS_WATCH
// Stop uploading
[self endBackgroundTaskIfNeeded];
_uploadTaskID = [app beginBackgroundTaskWithExpirationHandler:^{
@@ -1144,7 +1143,6 @@ - (void)enterBackground {
#endif

[self runOnBackgroundQueue:^{
self->_inForeground = NO;
[self refreshSessionTime:now];
[self uploadEventsWithLimit:0];
}];
@@ -1173,8 +1171,8 @@ - (void)endBackgroundTaskIfNeeded {
*
* Returns YES if a new session was created.
*/
- (BOOL)startOrContinueSessionNSNumber:(NSNumber *)timestamp {
if (!_inForeground) {
- (BOOL)startOrContinueSessionNSNumber:(NSNumber *)timestamp inForeground:(BOOL) inForeground {
if (!inForeground) {
if ([self inSession]) {
if ([self isWithinMinTimeBetweenSessions:timestamp]) {
[self refreshSessionTime:timestamp];
@@ -1207,7 +1205,7 @@ - (BOOL)startOrContinueSessionNSNumber:(NSNumber *)timestamp {

- (BOOL)startOrContinueSession:(long long)timestamp {
NSNumber *timestampNumber = [NSNumber numberWithLongLong:timestamp];
return [self startOrContinueSessionNSNumber:timestampNumber];
return [self startOrContinueSessionNSNumber:timestampNumber inForeground:_inForeground];
}

- (void)startNewSession:(NSNumber *)timestamp {