Skip to content

Commit 88a7ce0

Browse files
authored
fix: remove timer fix implementation for session (#7)
* fix: remove timer fix implementation for session * fix timing logic, remove sessionTimer * remove background event and timer --------- Co-authored-by: Alan Charles <[email protected]>
1 parent bf37dee commit 88a7ce0

File tree

2 files changed

+38
-59
lines changed

2 files changed

+38
-59
lines changed

Example/BasicExample/BasicExample/BasicExampleApp.swift

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,17 @@ struct BasicExampleApp: App {
1818
}
1919
}
2020

21+
var instance: Analytics? = nil
22+
2123
extension Analytics {
2224
static var main: Analytics {
23-
let analytics = Analytics(configuration: Configuration(writeKey: "<YOUR WRITE KEY>")
24-
.flushAt(3)
25-
.trackApplicationLifecycleEvents(true))
26-
analytics.add(plugin: AmplitudeSession())
27-
return analytics
25+
if instance == nil {
26+
instance = Analytics(configuration: Configuration(writeKey: "<WRITE_KEY>")
27+
.flushAt(3)
28+
.trackApplicationLifecycleEvents(true))
29+
instance?.add(plugin: AmplitudeSession())
30+
}
31+
32+
return instance!
2833
}
2934
}

Sources/SegmentAmplitude/AmplitudeSession.swift

Lines changed: 28 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,16 @@ public class AmplitudeSession: EventPlugin, iOSLifecycle {
4242

4343
var active = false
4444

45-
private var sessionTimer: Timer?
4645
private var sessionID: TimeInterval?
47-
private let fireTime = TimeInterval(300)
46+
private var lastEventFiredTime = Date()
47+
private var minSessionTime: TimeInterval = 5 * 60
4848

49-
public init() { }
49+
public init() {
50+
if (sessionID == nil || sessionID == -1)
51+
{
52+
sessionID = Date().timeIntervalSince1970
53+
}
54+
}
5055

5156
public func update(settings: Settings, type: UpdateType) {
5257
if settings.hasIntegrationSettings(key: key) {
@@ -61,20 +66,22 @@ public class AmplitudeSession: EventPlugin, iOSLifecycle {
6166
return event
6267
}
6368

69+
lastEventFiredTime = Date()
70+
6471
var result: T? = event
6572
switch result {
66-
case let r as IdentifyEvent:
67-
result = self.identify(event: r) as? T
68-
case let r as TrackEvent:
69-
result = self.track(event: r) as? T
70-
case let r as ScreenEvent:
71-
result = self.screen(event: r) as? T
72-
case let r as AliasEvent:
73-
result = self.alias(event: r) as? T
74-
case let r as GroupEvent:
75-
result = self.group(event: r) as? T
76-
default:
77-
break
73+
case let r as IdentifyEvent:
74+
result = self.identify(event: r) as? T
75+
case let r as TrackEvent:
76+
result = self.track(event: r) as? T
77+
case let r as ScreenEvent:
78+
result = self.screen(event: r) as? T
79+
case let r as AliasEvent:
80+
result = self.alias(event: r) as? T
81+
case let r as GroupEvent:
82+
result = self.group(event: r) as? T
83+
default:
84+
break
7885
}
7986
return result
8087
}
@@ -115,21 +122,24 @@ public class AmplitudeSession: EventPlugin, iOSLifecycle {
115122
}
116123

117124
public func applicationWillEnterForeground(application: UIApplication?) {
118-
startTimer()
125+
if Date().timeIntervalSince(lastEventFiredTime) >= minSessionTime {
126+
sessionID = Date().timeIntervalSince1970
127+
}
128+
119129
analytics?.log(message: "Amplitude Session ID: \(sessionID ?? -1)")
120130
}
121-
131+
122132
public func applicationWillResignActive(application: UIApplication?) {
123133
// Exposed if reacting to lifecycle events is needed
124134
}
135+
125136
}
126137

127138

128139
// MARK: - AmplitudeSession Helper Methods
129140
extension AmplitudeSession {
130141
func insertSession(event: RawEvent) -> RawEvent {
131142
var returnEvent = event
132-
refreshSessionID()
133143
if var integrations = event.integrations?.dictionaryValue,
134144
let sessionID = sessionID {
135145

@@ -138,42 +148,6 @@ extension AmplitudeSession {
138148
}
139149
return returnEvent
140150
}
141-
142-
@objc
143-
func handleTimerFire(_ timer: Timer) {
144-
stopTimer()
145-
}
146-
147-
func refreshSessionID() {
148-
if (sessionID == nil || sessionID == -1)
149-
{
150-
sessionID = Date().timeIntervalSince1970
151-
}
152-
startTimer()
153-
}
154-
155-
func startTimer() {
156-
// Starting and stopping the timer has to be done from the same thread; always dispatch to main queue
157-
DispatchQueue.main.async {
158-
self.sessionTimer?.invalidate()
159-
self.sessionTimer = Timer(timeInterval: self.fireTime, target: self,
160-
selector: #selector(self.handleTimerFire(_:)),
161-
userInfo: nil, repeats: true)
162-
self.sessionTimer?.tolerance = 0.3
163-
if let sessionTimer = self.sessionTimer {
164-
// Use the RunLoop current to avoid retaining self
165-
RunLoop.current.add(sessionTimer, forMode: .common)
166-
}
167-
}
168-
}
169-
170-
func stopTimer() {
171-
// Starting and stopping the timer has to be done from the same thread; always dispatch to main queue
172-
DispatchQueue.main.async {
173-
self.sessionTimer?.invalidate()
174-
self.sessionID = -1
175-
}
176-
}
177151
}
178152

179153
extension AmplitudeSession: VersionedPlugin {

0 commit comments

Comments
 (0)