diff --git a/Sources/Confidence/Confidence.swift b/Sources/Confidence/Confidence.swift index c7c28c5b..ff4554bf 100644 --- a/Sources/Confidence/Confidence.swift +++ b/Sources/Confidence/Confidence.swift @@ -129,6 +129,9 @@ public class Confidence: ConfidenceEventSender { return } self.track(eventName: event.name, message: event.message) + if event.shouldFlush { + eventSenderEngine.flush() + } } .store(in: &cancellables) } diff --git a/Sources/Confidence/ConfidenceAppLifecycleProducer.swift b/Sources/Confidence/ConfidenceAppLifecycleProducer.swift index 944d975a..4257e555 100644 --- a/Sources/Confidence/ConfidenceAppLifecycleProducer.swift +++ b/Sources/Confidence/ConfidenceAppLifecycleProducer.swift @@ -60,7 +60,7 @@ public class ConfidenceAppLifecycleProducer: ConfidenceEventProducer, Confidence .eraseToAnyPublisher() } - private func track(eventName: String) { + private func track(eventName: String, shouldFlush: Bool) { let previousBuild: String? = UserDefaults.standard.string(forKey: Self.userDefaultBuildNameKey) let previousVersion: String? = UserDefaults.standard.string(forKey: Self.userDefaultVersionNameKey) @@ -69,12 +69,20 @@ public class ConfidenceAppLifecycleProducer: ConfidenceEventProducer, Confidence if eventName == Self.appLaunchedEventName { if previousBuild == nil && previousVersion == nil { - events.send(Event(name: ConfidenceAppLifecycleProducer.appInstalledEventName, message: [:])) + events.send(Event( + name: ConfidenceAppLifecycleProducer.appInstalledEventName, + message: [:], + shouldFlush: shouldFlush) + ) } else if previousBuild != currentBuild || previousVersion != currentVersion { - events.send(Event(name: ConfidenceAppLifecycleProducer.appUpdatedEventName, message: [:])) + events.send(Event( + name: ConfidenceAppLifecycleProducer.appUpdatedEventName, + message: [:], + shouldFlush: shouldFlush) + ) } } - events.send(Event(name: eventName, message: [:])) + events.send(Event(name: eventName, message: [:], shouldFlush: shouldFlush)) UserDefaults.standard.setValue(currentVersion, forKey: Self.userDefaultVersionNameKey) UserDefaults.standard.setValue(currentBuild, forKey: Self.userDefaultBuildNameKey) @@ -104,7 +112,7 @@ public class ConfidenceAppLifecycleProducer: ConfidenceEventProducer, Confidence case UIApplication.willEnterForegroundNotification: updateContext(isForeground: true) case UIApplication.didBecomeActiveNotification: - track(eventName: Self.appLaunchedEventName) + track(eventName: Self.appLaunchedEventName, shouldFlush: true) default: break } diff --git a/Sources/Confidence/ConfidenceProducer.swift b/Sources/Confidence/ConfidenceProducer.swift index fc55e0f4..57074309 100644 --- a/Sources/Confidence/ConfidenceProducer.swift +++ b/Sources/Confidence/ConfidenceProducer.swift @@ -10,10 +10,12 @@ public protocol ConfidenceProducer { public struct Event { let name: String let message: ConfidenceStruct + let shouldFlush: Bool - public init(name: String, message: ConfidenceStruct = [:]) { + public init(name: String, message: ConfidenceStruct = [:], shouldFlush: Bool = false) { self.name = name self.message = message + self.shouldFlush = shouldFlush } }