Skip to content

Commit

Permalink
Handle deleting health events on app version change, and handling a c…
Browse files Browse the repository at this point in the history
…rash due to 'Unexpectedly found nil while implicitly unwrapping an Optional value'
  • Loading branch information
AbhijeetMallick committed Apr 26, 2024
1 parent 09ae083 commit cb02d8e
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 8 deletions.
18 changes: 13 additions & 5 deletions Sources/Tracker/Health/HealthTracker.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,17 @@ final class HealthTracker {
queue.async {
let trackeVia: TrackedVia = Tracker.healthTrackingConfigs.trackedVia == .both ? .both : .external

var events: [HealthAnalysisEvent]!
var healthAnalysisEvents: [HealthAnalysisEvent]?
if Tracker.healthTrackingConfigs.trackedVia == .both {
events = self._persistence.fetchAll()
healthAnalysisEvents = self._persistence.fetchAll()

} else {
events = self._persistence.deleteWhere(HealthAnalysisEvent.Columns.trackedVia,
healthAnalysisEvents = self._persistence.deleteWhere(HealthAnalysisEvent.Columns.trackedVia,
value: trackeVia.rawValue)
}

guard !events.isEmpty else { return }
guard let events = healthAnalysisEvents, !events.isEmpty else {
return
}

let instantEvents = events.filter { $0.eventType.rawValue == TrackerConstant.EventType.instant.rawValue }
for instantEvent in instantEvents {
Expand Down Expand Up @@ -108,4 +110,10 @@ final class HealthTracker {
}
return nil
}

func deleteHealthDataOnAppUpgrade() {
if let doesTableExist = _persistence.doesTableExist(with: HealthAnalysisEvent.description), doesTableExist {
_persistence.deleteAll()
}
}
}
7 changes: 4 additions & 3 deletions Sources/Tracker/Tracker.swift
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public final class Tracker {
self._dataSource = dataSource
self.delegate = delegate
self.observeAppStateChanges()
self.flushOnAppUpgrade()
self.deleteHealthDataOnAppUpgrade()
}

@discardableResult
Expand Down Expand Up @@ -283,12 +283,13 @@ public final class Tracker {
}

/// Flushes health events, being tracked via Clickstream, in case of an app upgrade.
private func flushOnAppUpgrade() {
private func deleteHealthDataOnAppUpgrade() {
Tracker.queue.async { [weak self] in guard let checkedSelf = self else { return }
let appVersionChecker = DefaultAppVersionChecker(currentAppVersion: checkedSelf.commonProperties?.app.version)
if appVersionChecker.hasAppVersionChanged() {
print("hasAppVersionChanged yes ", .critical)
// Delete health events being tracked via Clickstream
checkedSelf.healthTracker.flushFunnelEvents()
checkedSelf.healthTracker.deleteHealthDataOnAppUpgrade()
}
}
}
Expand Down

0 comments on commit cb02d8e

Please sign in to comment.