Skip to content

Commit 1d1f66e

Browse files
authored
[Sessions] Fix how Session ID is generated (#10430)
1 parent c7386ca commit 1d1f66e

File tree

4 files changed

+33
-22
lines changed

4 files changed

+33
-22
lines changed

FirebaseSessions/Sources/Identifiers.swift

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,6 @@ import Foundation
1717

1818
@_implementationOnly import FirebaseInstallations
1919

20-
let sessionIDUserDefaultsKey = "com.firebase.sessions.sessionID"
21-
let lastSessionIDUserDefaultsKey = "com.firebase.sessions.lastSessionID"
22-
2320
protocol IdentifierProvider {
2421
var installationID: String {
2522
get
@@ -29,7 +26,7 @@ protocol IdentifierProvider {
2926
get
3027
}
3128

32-
var previousSessionID: String {
29+
var previousSessionID: String? {
3330
get
3431
}
3532
}
@@ -44,23 +41,18 @@ protocol IdentifierProvider {
4441
class Identifiers: IdentifierProvider {
4542
private let installations: InstallationsProtocol
4643

47-
private var _sessionID: UUID
44+
private var _sessionID: String?
45+
private var _previousSessionID: String?
4846

4947
init(installations: InstallationsProtocol) {
5048
self.installations = installations
51-
_sessionID = UUID()
5249
}
5350

5451
// Generates a new Session ID. If there was already a generated Session ID
5552
// from the last session during the app's lifecycle, it will also set the last Session ID
5653
func generateNewSessionID() {
57-
_sessionID = UUID()
58-
59-
let lastStoredSessionID = UserDefaults.standard.string(forKey: sessionIDUserDefaultsKey) ?? ""
60-
UserDefaults.standard.set(lastStoredSessionID, forKey: lastSessionIDUserDefaultsKey)
61-
62-
let newSessionID = _sessionID.uuidString.replacingOccurrences(of: "-", with: "").lowercased()
63-
UserDefaults.standard.set(newSessionID, forKey: sessionIDUserDefaultsKey)
54+
_previousSessionID = _sessionID
55+
_sessionID = UUID().uuidString.replacingOccurrences(of: "-", with: "").lowercased()
6456
}
6557

6658
// Fetches the Installation ID from Firebase Installation Service. This method must be run on a background thread due to how Firebase Installations
@@ -103,10 +95,14 @@ class Identifiers: IdentifierProvider {
10395
}
10496

10597
var sessionID: String {
106-
return UserDefaults.standard.string(forKey: sessionIDUserDefaultsKey) ?? ""
98+
guard let _sessionID = _sessionID else {
99+
Logger.logError("Error: Sessions SDK did not generate a Session ID")
100+
return ""
101+
}
102+
return _sessionID
107103
}
108104

109-
var previousSessionID: String {
110-
return UserDefaults.standard.string(forKey: lastSessionIDUserDefaultsKey) ?? ""
105+
var previousSessionID: String? {
106+
return _previousSessionID
111107
}
112108
}

FirebaseSessions/Sources/SessionStartEvent.swift

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ class SessionStartEvent: NSObject, GDTCOREventDataObject {
3333

3434
proto.event_type = firebase_appquality_sessions_EventType_SESSION_START
3535
proto.session_data.session_id = makeProtoString(identifiers.sessionID)
36-
proto.session_data.previous_session_id = makeProtoString(identifiers.previousSessionID)
36+
proto.session_data.previous_session_id = makeProtoStringOrNil(identifiers.previousSessionID)
3737
proto.session_data.event_timestamp_us = time.timestampUS
3838

3939
proto.application_info.app_id = makeProtoString(appInfo.appID)
@@ -70,6 +70,13 @@ class SessionStartEvent: NSObject, GDTCOREventDataObject {
7070

7171
// MARK: - Data Conversion
7272

73+
private func makeProtoStringOrNil(_ string: String?) -> UnsafeMutablePointer<pb_bytes_array_t>? {
74+
guard let string = string else {
75+
return nil
76+
}
77+
return FIRSESEncodeString(string)
78+
}
79+
7380
private func makeProtoString(_ string: String) -> UnsafeMutablePointer<pb_bytes_array_t>? {
7481
return FIRSESEncodeString(string)
7582
}

FirebaseSessions/Tests/Unit/IdentifiersTests.swift

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,18 @@ class IdentifiersTests: XCTestCase {
4848
return true
4949
}
5050

51+
// This test case isn't important behavior. When Crash and Perf integrate
52+
// with the Sessions SDK, we may want to move to a lazy solution where
53+
// sessionID can never be empty
54+
func test_sessionID_beforeGenerateReturnsNothing() throws {
55+
XCTAssert(identifiers.sessionID.count == 0)
56+
XCTAssertNil(identifiers.previousSessionID)
57+
}
58+
5159
func test_generateNewSessionID_generatesValidID() throws {
5260
identifiers.generateNewSessionID()
5361
XCTAssert(isValidSessionID(identifiers.sessionID))
54-
XCTAssert(identifiers.previousSessionID.count == 0)
62+
XCTAssertNil(identifiers.previousSessionID)
5563
}
5664

5765
/// Ensures that generating a Session ID multiple times results in the last Session ID being set in the previousSessionID field
@@ -60,15 +68,15 @@ class IdentifiersTests: XCTestCase {
6068

6169
let firstSessionID = identifiers.sessionID
6270
XCTAssert(isValidSessionID(identifiers.sessionID))
63-
XCTAssert(identifiers.previousSessionID.count == 0)
71+
XCTAssertNil(identifiers.previousSessionID)
6472

6573
identifiers.generateNewSessionID()
6674

6775
XCTAssert(isValidSessionID(identifiers.sessionID))
68-
XCTAssert(isValidSessionID(identifiers.previousSessionID))
76+
XCTAssert(isValidSessionID(identifiers.previousSessionID!))
6977

7078
// Ensure the new lastSessionID is equal to the sessionID from earlier
71-
XCTAssert(identifiers.previousSessionID.compare(firstSessionID) == ComparisonResult.orderedSame)
79+
XCTAssertEqual(identifiers.previousSessionID, firstSessionID)
7280
}
7381

7482
// Fetching FIIDs requires that we are on a background thread.

FirebaseSessions/Tests/Unit/Mocks/MockIdentifierProvider.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import Foundation
2020
class MockIdentifierProvider: IdentifierProvider {
2121
var sessionID: String = ""
2222

23-
var previousSessionID: String = ""
23+
var previousSessionID: String?
2424

2525
var installationID: String = ""
2626

0 commit comments

Comments
 (0)