Skip to content

Commit

Permalink
fix reset session when reset or close are called
Browse files Browse the repository at this point in the history
  • Loading branch information
marandaneto committed Feb 16, 2024
1 parent 2cd6517 commit 99db621
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
## Next

- fix reset session when reset or close are called [#106](https://github.com/PostHog/posthog-ios/pull/106)

## 3.1.3 - 2024-02-09

- fix ISO8601 formatter to always use the 24h format [#106](https://github.com/PostHog/posthog-ios/pull/106)
Expand Down
33 changes: 33 additions & 0 deletions PostHog/PostHogSDK.swift
Original file line number Diff line number Diff line change
Expand Up @@ -245,9 +245,18 @@ private let sessionChangeThreshold: TimeInterval = 60 * 30
return
}

// storage also removes all feature flags
storage?.reset()
queue?.clear()
flagCallReported.removeAll()
resetSession()
}

private func resetSession() {
sessionLock.withLock {
sessionId = nil
sessionLastTimestamp = nil
}
}

private func getGroups() -> [String: String] {
Expand Down Expand Up @@ -674,12 +683,22 @@ private let sessionChangeThreshold: TimeInterval = 60 * 30
sessionManager = nil
config = PostHogConfig(apiKey: "")
api = nil
featureFlags = nil
storage = nil
#if !os(watchOS)
self.reachability?.stopNotifier()
reachability = nil
#endif
flagCallReported.removeAll()
featureFlags = nil
context = nil
resetSession()
unregisterNotifications()
capturedAppInstalled = false
appFromBackground = false
isInBackground = false
toggleHedgeLog(false)
// TODO: remove swizzlers
}
}

Expand All @@ -689,6 +708,20 @@ private let sessionChangeThreshold: TimeInterval = 60 * 30
return postHog
}

private func unregisterNotifications() {
let defaultCenter = NotificationCenter.default

#if os(iOS) || os(tvOS)
defaultCenter.removeObserver(self, name: UIApplication.didFinishLaunchingNotification, object: nil)
defaultCenter.removeObserver(self, name: UIApplication.didEnterBackgroundNotification, object: nil)
defaultCenter.removeObserver(self, name: UIApplication.didBecomeActiveNotification, object: nil)
#elseif os(macOS)
defaultCenter.removeObserver(self, name: NSApplication.didFinishLaunchingNotification, object: nil)
defaultCenter.removeObserver(self, name: NSApplication.didResignActiveNotification, object: nil)
defaultCenter.removeObserver(self, name: NSApplication.didBecomeActiveNotification, object: nil)
#endif
}

private func registerNotifications() {
let defaultCenter = NotificationCenter.default

Expand Down

0 comments on commit 99db621

Please sign in to comment.