Skip to content

Commit d876770

Browse files
authored
Merge pull request #19 from Claeysson/async
Removed semaphore.wait() and introduced a timer pause boolean.
2 parents 02f791e + 3a4bce8 commit d876770

File tree

1 file changed

+14
-9
lines changed

1 file changed

+14
-9
lines changed

Sources/Aptabase/AptabaseClient.swift

+14-9
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ class AptabaseClient {
1111
private let dispatcher: EventDispatcher
1212
private let env: EnvironmentInfo
1313
private let flushInterval: Double
14+
private var pauseFlushTimer: Bool = false
1415

1516
init(appKey: String, baseUrl: String, env: EnvironmentInfo, options: InitOptions?) {
1617
flushInterval = options?.flushInterval ?? (env.isDebug ? 2.0 : 60.0)
@@ -46,14 +47,17 @@ class AptabaseClient {
4647
public func startPolling() {
4748
stopPolling()
4849

49-
flushTimer = Timer.scheduledTimer(timeInterval: flushInterval, target: self, selector: #selector(flushSync), userInfo: nil, repeats: true)
50+
flushTimer = Timer.scheduledTimer(timeInterval: flushInterval, target: self, selector: #selector(timerFlushSync), userInfo: nil, repeats: true)
5051
}
5152

5253
public func stopPolling() {
5354
flushTimer?.invalidate()
5455
flushTimer = nil
55-
56-
flushSync()
56+
57+
Task {
58+
await flush()
59+
}
60+
5761
}
5862

5963
public func flush() async {
@@ -66,12 +70,13 @@ class AptabaseClient {
6670
return String(epochInSeconds * 100000000 + random)
6771
}
6872

69-
@objc private func flushSync() {
70-
let semaphore = DispatchSemaphore(value: 0)
71-
Task {
72-
await self.flush()
73-
semaphore.signal()
73+
@objc private func timerFlushSync() {
74+
if !pauseFlushTimer {
75+
Task {
76+
self.pauseFlushTimer = true
77+
await self.flush()
78+
self.pauseFlushTimer = false
79+
}
7480
}
75-
semaphore.wait()
7681
}
7782
}

0 commit comments

Comments
 (0)