Skip to content

Commit

Permalink
fix: set _inForeground value earlier (#433)
Browse files Browse the repository at this point in the history
* fix: set _inForeground value earlier

* fix: move _inForeground setter into checkInForeground block

* fix: move _inForeground setter after 'state != UIApplicationStateBackground' check

* chore: update runners to mac-12

* chore: update runners to xcode 14

* chore: update runners to xcode 14, again

* chore: update runners to xcode 14, again

---------

Co-authored-by: justin.fiedler <justin.fiedler@amplitude.com>
  • Loading branch information
falconandy and justin-fiedler authored May 26, 2023
1 parent e818b18 commit 3cee4c2
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 26 deletions.
10 changes: 5 additions & 5 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ on:
jobs:
authorize:
name: Authorize
runs-on: macos-10.15
runs-on: macos-12
steps:
- name: ${{ github.actor }} permission check to do a release
uses: octokit/request-action@v2.0.0
Expand All @@ -23,7 +23,7 @@ jobs:

release:
name: Release
runs-on: macos-10.15
runs-on: macos-12
needs: [authorize]
strategy:
matrix:
Expand All @@ -33,9 +33,9 @@ jobs:
- name: Checkout Amplitude-iOS
uses: actions/checkout@v2

- name: Set Xcode 12
- name: Set Xcode 14
run: |
sudo xcode-select -switch /Applications/Xcode_12.app
sudo xcode-select -switch /Applications/Xcode_14.1.app
- name: Carthage Bootstrap
run: carthage bootstrap --use-xcframeworks
Expand Down Expand Up @@ -68,7 +68,7 @@ jobs:
-workspace Amplitude.xcworkspace \
-scheme Amplitude_iOSTests \
-sdk iphonesimulator \
-destination 'platform=iOS Simulator,name=iPhone 11,OS=14.0'
-destination 'platform=iOS Simulator,name=iPhone 14'
# - name: macOS Tests @TODO Fix flaky macOS tests and re-enable
# run: |
Expand Down
8 changes: 4 additions & 4 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,17 @@ on: [push, pull_request]
jobs:
test:
name: Test
runs-on: macos-10.15
runs-on: macos-12
strategy:
matrix:
ruby-version: ["2.7.x"]
steps:
- name: Check out Git repository
uses: actions/checkout@v2

- name: Set Xcode 12
- name: Set Xcode 14
run: |
sudo xcode-select -switch /Applications/Xcode_12.app
sudo xcode-select -switch /Applications/Xcode_14.1.app
- name: Carthage Bootstrap
run: carthage bootstrap --use-xcframeworks
Expand Down Expand Up @@ -51,7 +51,7 @@ jobs:
-workspace Amplitude.xcworkspace \
-scheme Amplitude_iOSTests \
-sdk iphonesimulator \
-destination 'platform=iOS Simulator,name=iPhone 11,OS=14.0'
-destination 'platform=iOS Simulator,name=iPhone 14'
# - name: macOS Tests @TODO Fix flaky macOS tests and re-enable
# run: |
Expand Down
33 changes: 16 additions & 17 deletions Sources/Amplitude/Amplitude.m
Original file line number Diff line number Diff line change
Expand Up @@ -495,14 +495,16 @@ - (void) checkInForeground {
if (app != nil) {
UIApplicationState state = app.applicationState;
if (state != UIApplicationStateBackground) {
self->_inForeground = YES;
[self runOnBackgroundQueue:^{
#else
self->_inForeground = YES;
#endif
// The earliest time to fetch dynamic config
[self refreshDynamicConfig];

NSNumber *now = [NSNumber numberWithLongLong:[[self currentTime] timeIntervalSince1970] * 1000];
[self startOrContinueSessionNSNumber:now];
self->_inForeground = YES;
[self startOrContinueSessionNSNumber:now inForeground:NO];
#if !TARGET_OS_OSX && !TARGET_OS_WATCH
}];

Expand Down Expand Up @@ -610,6 +612,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.
Expand All @@ -621,7 +624,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];
Expand Down Expand Up @@ -1122,40 +1125,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:^{
Expand All @@ -1165,7 +1165,6 @@ - (void)enterBackground {
#endif

[self runOnBackgroundQueue:^{
self->_inForeground = NO;
[self refreshSessionTime:now];
[self uploadEventsWithLimit:0];
}];
Expand Down Expand Up @@ -1194,8 +1193,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];
Expand Down Expand Up @@ -1228,7 +1227,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 {
Expand Down

0 comments on commit 3cee4c2

Please sign in to comment.