segment-integrations / analytics-swift-amplitude

Analytics-Swift Amplitude Session Plugin

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Crash in AmplitudeSession.startTimer()

sebastienwindal opened this issue · comments

I am having a crash in AmplitudeSession.startTimer(), here is the full stacktrace:

0
CoreFoundation
_CFRetain
1
CoreFoundation
_CFRunLoopTimerInvalidate
2
Mercury
AmplitudeSession.swift:151:23
AmplitudeSession.startTimer()
3
Mercury
AmplitudeSession.swift:147:9
AmplitudeSession.refreshSessionID()
4
Mercury
AmplitudeSession.swift:78:33
AmplitudeSession.track(event:)
5
Mercury
Timeline.swift:80:37
specialized closure #1 in Mediator.execute<A>(event:)
6
Mercury
Timeline.swift:32:32
specialized Timeline.process<A>(incomingEvent:)
7
Mercury
Events.swift:155:9
Analytics.track(name:properties:)
8
Mercury
SegmentProvider.swift:33:19
SegmentProvider.track(_:properties:)

this is happening on a background thread. When a call to track() I do on a background thread triggers a AmplitudeSession.refreshSessionID() which in turns invalidates the timer.

I am guessing the issue is that timer was started on the main thread (applicationWillEnterForeground) and calling invalidate on a different thread is the reason for the crash. According to apple:

https://developer.apple.com/documentation/foundation/timer/1415405-invalidate

You must send this message from the thread on which the timer was installed. If you send this message from another thread, the input source associated with the timer may not be removed from its run loop, which could prevent the thread from exiting properly.

Please advise, I would hate to have to do all my tracking on the main thread.