Skip to content

Commit

Permalink
Clearinstance added
Browse files Browse the repository at this point in the history
  • Loading branch information
asenthil-altmtrk committed Mar 19, 2020
1 parent 04b4ecf commit ba1ca02
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 11 deletions.
4 changes: 4 additions & 0 deletions Sources/HttpClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ struct HttpClient {
self.session = HttpClient.createUrlSession()
}

func invalidateSession() {
session.invalidateAndCancel()
}

func post(with url: String, httpBody: Data, completionHandler: @escaping (Data?, URLResponse?, Error?) -> Void) {
if let url = URL(string: url) {
var request = URLRequest(url: url)
Expand Down
28 changes: 17 additions & 11 deletions Sources/Insights.swift
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,13 @@ public final class Insights: InsightsProtocol {
self.userToken = userToken
visitId = UUID().uuidString
}

deinit {
/// Clears Insights instance.
public static func clearInstance() {
if let httpClient = instance?.httpClient {
httpClient.invalidateSession()
}
removeObserversForTrackingLifecycleEvents()
instance = nil
}

/// Set up the shared instance of the Insights framework
Expand All @@ -131,13 +135,15 @@ public final class Insights: InsightsProtocol {
sdkVersion: String,
apiUrl: String,
userToken: String) {
instance = Insights(environment: environment,
programToken: programToken,
sdkVersion: sdkVersion,
apiUrl: apiUrl,
userToken: userToken)

instance?.addObserversForTrackingLifecycleEvents()
if instance == nil {
instance = Insights(environment: environment,
programToken: programToken,
sdkVersion: sdkVersion,
apiUrl: apiUrl,
userToken: userToken)

addObserversForTrackingLifecycleEvents()
}
}

public func trackClick(pageName: String, pageGroup: String, link: String, params: [String: String]) {
Expand Down Expand Up @@ -168,7 +174,7 @@ public final class Insights: InsightsProtocol {
}
}

private func addObserversForTrackingLifecycleEvents() {
private static func addObserversForTrackingLifecycleEvents() {
let application = UIApplication.shared
NotificationCenter.default.addObserver(self,
selector: #selector(didEnterBackground(_:)),
Expand All @@ -180,7 +186,7 @@ public final class Insights: InsightsProtocol {
object: application)
}

private func removeObserversForTrackingLifecycleEvents() {
private static func removeObserversForTrackingLifecycleEvents() {
let application = UIApplication.shared
NotificationCenter.default.removeObserver(self,
name: UIApplication.didEnterBackgroundNotification,
Expand Down
1 change: 1 addition & 0 deletions Tests/InsightsTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ final class InsightsTests: XCTestCase {
override func setUp() {
super.setUp()
EventManager.shared.deleteEvents(before: Date().epochMilliseconds())
Insights.clearInstance()
Insights.setup(environment: environment,
programToken: programToken,
sdkVersion: sdkVersion,
Expand Down

0 comments on commit ba1ca02

Please sign in to comment.