Skip to content

Commit 2bd56fb

Browse files
author
Guilherme Souza
committed
Add timer for sending logs to the backend
1 parent 5e61c9c commit 2bd56fb

File tree

1 file changed

+47
-3
lines changed

1 file changed

+47
-3
lines changed

Sources/SupabaseLogger/SupabaseLogHandler.swift

Lines changed: 47 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,22 @@ public struct SupabaseLogConfig: Hashable {
1212
let supabaseAnonKey: String
1313

1414
let table: String
15+
let isDebug: Bool
1516

1617
public init(
1718
supabaseURL: String,
1819
supabaseAnonKey: String,
19-
table: String = "logs"
20+
table: String = "logs",
21+
isDebug: Bool = true
2022
) {
2123
self.supabaseURL = supabaseURL
2224
self.supabaseAnonKey = supabaseAnonKey
2325
self.table = table
26+
#if DEBUG
27+
self.isDebug = isDebug
28+
#else
29+
self.isDebug = false
30+
#endif
2431
}
2532
}
2633

@@ -68,6 +75,9 @@ final class SupabaseLogManager {
6875
let cache: LogsCache
6976
let config: SupabaseLogConfig
7077

78+
private let minimumWaitTimeBetweenRequests: TimeInterval = 10
79+
private var sendTimer: Timer?
80+
7181
private static let queue = DispatchQueue(
7282
label: "co.binaryscraping.supabase-log-manager.instances")
7383
private static var instances: [SupabaseLogConfig: SupabaseLogManager] = [:]
@@ -103,15 +113,29 @@ final class SupabaseLogManager {
103113
name: UIApplication.didEnterBackgroundNotification, object: nil)
104114
}
105115
#endif
116+
117+
startTimer()
118+
}
119+
120+
private func startTimer() {
121+
sendTimer?.invalidate()
122+
sendTimer = Timer.scheduledTimer(
123+
timeInterval: minimumWaitTimeBetweenRequests, target: self,
124+
selector: #selector(checkForLogsAndSend), userInfo: nil, repeats: true)
125+
126+
// Fire the timer to attempt to send any cached logs from a previous session.
127+
checkForLogsAndSend()
106128
}
107129

108130
func log(_ payload: [String: Any]) {
109131
cache.push(payload)
110132
}
111133

134+
@objc
112135
private func checkForLogsAndSend() {
113136
let logs = cache.pop()
114-
if logs.isEmpty { return }
137+
138+
guard !logs.isEmpty else { return }
115139

116140
let data = try! JSONSerialization.data(withJSONObject: logs)
117141
guard
@@ -142,7 +166,11 @@ final class SupabaseLogManager {
142166
throw URLError(.badServerResponse)
143167
}
144168
} catch {
145-
print(error)
169+
if self.config.isDebug {
170+
print(error)
171+
}
172+
173+
// An error ocurred, put logs back in cache.
146174
self.cache.push(logs)
147175
}
148176
}
@@ -152,14 +180,30 @@ final class SupabaseLogManager {
152180

153181
extension SupabaseLogManager {
154182
@objc func appWillTerminate() {
183+
if config.isDebug {
184+
print(#function)
185+
}
186+
155187
cache.backupCache()
156188
}
157189

158190
#if os(iOS)
159191
@objc func didEnterForeground() {
192+
if config.isDebug {
193+
print(#function)
194+
}
195+
196+
startTimer()
160197
}
161198

162199
@objc func didEnterBackground() {
200+
if config.isDebug {
201+
print(#function)
202+
}
203+
204+
sendTimer?.invalidate()
205+
sendTimer = nil
206+
163207
cache.backupCache()
164208
}
165209
#endif

0 commit comments

Comments
 (0)