Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid deadlock during first time initialisation #657

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion Sources/MixpanelInstance.swift
Original file line number Diff line number Diff line change
Expand Up @@ -909,7 +909,7 @@ extension MixpanelInstance {
}

func unarchive() {
self.readWriteLock.write {
let didCreateIdentity = self.readWriteLock.write {
optOutStatus = MixpanelPersistence.loadOptOutStatusFlag(instanceName: self.name)
superProperties = MixpanelPersistence.loadSuperProperties(instanceName: self.name)
timedEvents = MixpanelPersistence.loadTimedEvents(instanceName: self.name)
Expand All @@ -927,6 +927,14 @@ extension MixpanelInstance {
distinctId = addPrefixToDeviceId(deviceId: anonymousId)
hadPersistedDistinctId = true
userId = nil
return true
} else {
return false
}
}

if didCreateIdentity {
self.readWriteLock.read {
MixpanelPersistence.saveIdentity(MixpanelIdentity.init(
distinctID: distinctId,
peopleDistinctID: people.distinctId,
Expand Down
2 changes: 1 addition & 1 deletion Sources/ReadWriteLock.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class ReadWriteLock {
closure()
}
}
func write(closure: () -> Void) {
func write<T>(closure: () -> T) -> T {
concurrentQueue.sync(flags: .barrier, execute: {
closure()
})
Expand Down