Skip to content

Commit 7b616b1

Browse files
zhu-xiaoweixiaoweii
andauthored
fix: session start crash issue (#29)
Co-authored-by: xiaoweii <xiaoweii@amazom.com>
1 parent f1cc830 commit 7b616b1

File tree

4 files changed

+18
-18
lines changed

4 files changed

+18
-18
lines changed

Sources/Clickstream/AWSClickstreamPlugin+Configure.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ extension AWSClickstreamPlugin {
5050
clickstream.analyticsClient = analyticsClient
5151
let networkMonitor = NWPathMonitor()
5252
clickstream.networkMonitor = networkMonitor
53-
53+
sessionClient.initialSession()
5454
var autoFlushEventsTimer: DispatchSourceTimer?
5555
if configuration.sendEventsInterval != 0 {
5656
let timeInterval = TimeInterval(Double(configuration.sendEventsInterval) / 1_000)

Sources/Clickstream/Dependency/Clickstream/Session/SessionClient.swift

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import Foundation
1010

1111
protocol SessionClientBehaviour: AnyObject {
1212
func startActivityTracking()
13-
func initialSession() -> Bool
13+
func initialSession()
1414
func storeSession()
1515
}
1616

@@ -38,17 +38,19 @@ class SessionClient: SessionClientBehaviour {
3838
}
3939
}
4040

41-
func initialSession() -> Bool {
41+
func initialSession() {
4242
session = Session.getCurrentSession(clickstream: clickstream)
4343
if session!.isNewSession {
4444
autoRecordClient.recordSessionStartEvent()
45+
autoRecordClient.setIsEntrances()
4546
}
46-
return session!.isNewSession
4747
}
4848

4949
func storeSession() {
50-
session?.pause()
51-
UserDefaultsUtil.saveSession(storage: clickstream.storage, session: session!)
50+
if session != nil {
51+
session?.pause()
52+
UserDefaultsUtil.saveSession(storage: clickstream.storage, session: session!)
53+
}
5254
}
5355

5456
func getCurrentSession() -> Session? {
@@ -59,10 +61,7 @@ class SessionClient: SessionClientBehaviour {
5961
log.debug("Application entered the foreground.")
6062
autoRecordClient.handleAppStart()
6163
autoRecordClient.updateLastScreenStartTimestamp()
62-
let isNewSession = initialSession()
63-
if isNewSession {
64-
autoRecordClient.setIsEntrances()
65-
}
64+
initialSession()
6665
}
6766

6867
private func handleAppEnterBackground() {

Tests/ClickstreamTests/Clickstream/SessionClientTests.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,8 +198,9 @@ class SessionClientTests: XCTestCase {
198198
activityTracker.callback?(.runningInForeground)
199199
Thread.sleep(forTimeInterval: 1)
200200
activityTracker.callback?(.runningInBackground)
201+
Thread.sleep(forTimeInterval: 0.1)
201202
let events = eventRecorder.savedEvents
202-
XCTAssertEqual(7, events.count)
203+
XCTAssertEqual(8, events.count)
203204
let userEngagementEvent = events[6]
204205
XCTAssertEqual(Event.PresetEvent.USER_ENGAGEMENT, userEngagementEvent.eventType)
205206
XCTAssertTrue((userEngagementEvent.attributes[Event.ReservedAttribute.ENGAGEMENT_TIMESTAMP] as! Int64) < 1_200)

Tests/ClickstreamTests/IntegrationTest.swift

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -68,14 +68,14 @@ class IntegrationTest: XCTestCase {
6868
])
6969
Thread.sleep(forTimeInterval: 0.2)
7070
let eventCount = try eventRecorder.dbUtil.getEventCount()
71-
XCTAssertEqual(1, eventCount)
71+
XCTAssertEqual(2, eventCount)
7272
}
7373

7474
func testRecordOneEventWithNameSuccess() throws {
7575
ClickstreamAnalytics.recordEvent("testEvent")
7676
Thread.sleep(forTimeInterval: 0.2)
7777
let eventCount = try eventRecorder.dbUtil.getEventCount()
78-
XCTAssertEqual(1, eventCount)
78+
XCTAssertEqual(2, eventCount)
7979
}
8080

8181
func testFlushEvents() throws {
@@ -220,7 +220,7 @@ class IntegrationTest: XCTestCase {
220220
ClickstreamAnalytics.recordEvent("testEvent")
221221
Thread.sleep(forTimeInterval: 0.2)
222222
let eventCount = try eventRecorder.dbUtil.getEventCount()
223-
XCTAssertEqual(1, eventCount)
223+
XCTAssertEqual(2, eventCount)
224224
}
225225

226226
func testModifyConfiguration() throws {
@@ -233,7 +233,7 @@ class IntegrationTest: XCTestCase {
233233
])
234234
Thread.sleep(forTimeInterval: 0.2)
235235
let eventCount = try eventRecorder.dbUtil.getEventCount()
236-
XCTAssertEqual(1, eventCount)
236+
XCTAssertEqual(2, eventCount)
237237
}
238238

239239
// MARK: - Objc test
@@ -249,7 +249,7 @@ class IntegrationTest: XCTestCase {
249249
ClickstreamObjc.recordEvent("testEvent", attribute)
250250
Thread.sleep(forTimeInterval: 0.2)
251251
let eventCount = try eventRecorder.dbUtil.getEventCount()
252-
XCTAssertEqual(2, eventCount)
252+
XCTAssertEqual(3, eventCount)
253253
}
254254

255255
func testGlobalAttributeForObjc() throws {
@@ -298,14 +298,14 @@ class IntegrationTest: XCTestCase {
298298
ClickstreamAnalytics.recordEvent("testEvent")
299299
Thread.sleep(forTimeInterval: 0.2)
300300
let eventCount = try eventRecorder.dbUtil.getEventCount()
301-
XCTAssertEqual(1, eventCount)
301+
XCTAssertEqual(2, eventCount)
302302
}
303303

304304
func testAppException() throws {
305305
let exception = NSException(name: NSExceptionName("TestException"), reason: "Testing", userInfo: nil)
306306
AutoRecordEventClient.handleException(exception)
307307
Thread.sleep(forTimeInterval: 0.5)
308-
let event = try eventRecorder.getBatchEvent().eventsJson.jsonArray()[0]
308+
let event = try eventRecorder.getBatchEvent().eventsJson.jsonArray()[1]
309309
XCTAssertTrue(event["event_type"] as! String == Event.PresetEvent.APP_EXCEPTION)
310310
let attributes = event["attributes"] as! [String: Any]
311311
XCTAssertTrue(attributes[Event.ReservedAttribute.EXCEPTION_NAME] as! String == exception.name.rawValue)

0 commit comments

Comments
 (0)