@@ -74,7 +74,10 @@ constructor(
7474 Log .d(TAG , " App backgrounded on ${getProcessName()} - $sessionData " )
7575
7676 CoroutineScope (backgroundDispatcher).launch {
77- updateSessionData(sessionData.copy(backgroundTime = timeProvider.currentTime()))
77+ sessionDataStore.updateData {
78+ // TODO(mrober): Double check time makes sense?
79+ sessionData.copy(backgroundTime = timeProvider.currentTime())
80+ }
7881 }
7982 }
8083
@@ -86,14 +89,19 @@ constructor(
8689 val sessionData = localSessionData
8790 Log .d(TAG , " App foregrounded on ${getProcessName()} - $sessionData " )
8891
89- val interval = timeProvider.currentTime() - sessionData.backgroundTime
90- Log .d(TAG , " Interval: $interval " )
91- if (interval > sessionsSettings.sessionRestartTimeout) {
92+ if (shouldInitiateNewSession(sessionData)) {
9293 // Generate new session details on main thread so the timestamp is as current as possible
9394 val newSessionDetails = sessionGenerator.generateNewSession(sessionData.sessionDetails)
9495
9596 CoroutineScope (backgroundDispatcher).launch {
96- updateSessionData(sessionData.copy(sessionDetails = newSessionDetails))
97+ sessionDataStore.updateData { currentSessionData ->
98+ // Double-check pattern
99+ if (shouldInitiateNewSession(currentSessionData)) {
100+ currentSessionData.copy(sessionDetails = newSessionDetails)
101+ } else {
102+ currentSessionData
103+ }
104+ }
97105 }
98106
99107 // TODO(mrober): If data collection is enabled for at least one subscriber...
@@ -102,12 +110,9 @@ constructor(
102110 }
103111 }
104112
105- private suspend fun updateSessionData (sessionData : SessionData ) {
106- sessionDataStore.updateData {
107- // TODO(mrober): Do double-check pattern, might need two update methods
108- Log .d(TAG , " Update data to: $sessionData " )
109- sessionData
110- }
113+ private fun shouldInitiateNewSession (sessionData : SessionData ): Boolean {
114+ val interval = timeProvider.currentTime() - sessionData.backgroundTime
115+ return interval > sessionsSettings.sessionRestartTimeout
111116 }
112117
113118 private companion object {
0 commit comments