diff --git a/Sources/Amplitude/Amplitude.m b/Sources/Amplitude/Amplitude.m index 39dddc38..ac56ce87 100644 --- a/Sources/Amplitude/Amplitude.m +++ b/Sources/Amplitude/Amplitude.m @@ -1681,11 +1681,13 @@ - (long long)getSessionId { } - (NSString *)initializeDeviceId { - if (self.deviceId == nil) { - self.deviceId = [self.dbHelper getValue:DEVICE_ID]; - if (![self isValidDeviceId:self.deviceId]) { - self.deviceId = [self _getDeviceId]; - [self.dbHelper insertOrReplaceKeyValue:DEVICE_ID value:self.deviceId]; + @synchronized (self) { + if (self.deviceId == nil) { + self.deviceId = [self.dbHelper getValue:DEVICE_ID]; + if (![self isValidDeviceId:self.deviceId]) { + self.deviceId = [self _getDeviceId]; + [self.dbHelper insertOrReplaceKeyValue:DEVICE_ID value:self.deviceId]; + } } } return self.deviceId;