Skip to content

Commit d0ddd8f

Browse files
initialize user profile on its own thread. getting the bundle for each impression event is costing at least 3ms.
1 parent ae79255 commit d0ddd8f

File tree

2 files changed

+14
-4
lines changed

2 files changed

+14
-4
lines changed

OptimizelySDK/Customization/DefaultUserProfileService.swift

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,18 +74,21 @@ import Foundation
7474
open class DefaultUserProfileService: OPTUserProfileService {
7575
public typealias UserProfileData = [String: UPProfile]
7676

77-
var profiles: UserProfileData
77+
var profiles: UserProfileData?
7878
let lock = DispatchQueue(label: "com.optimizely.UserProfileService")
7979
let kStorageName = "user-profile-service"
8080

8181
public required init() {
82-
profiles = UserDefaults.standard.dictionary(forKey: kStorageName) as? UserProfileData ?? UserProfileData()
82+
lock.async {
83+
self.profiles = UserDefaults.standard.dictionary(forKey: self.kStorageName) as? UserProfileData ?? UserProfileData()
84+
85+
}
8386
}
8487

8588
open func lookup(userId: String) -> UPProfile? {
8689
var retVal: UPProfile?
8790
lock.sync {
88-
retVal = profiles[userId]
91+
retVal = profiles?[userId]
8992
}
9093
return retVal
9194
}
@@ -94,7 +97,7 @@ open class DefaultUserProfileService: OPTUserProfileService {
9497
guard let userId = userProfile[UserProfileKeys.kUserId] as? String else { return }
9598

9699
lock.async {
97-
self.profiles[userId] = userProfile
100+
self.profiles?[userId] = userProfile
98101
let defaults = UserDefaults.standard
99102
defaults.set(self.profiles, forKey: self.kStorageName)
100103
defaults.synchronize()

OptimizelySDK/Utils/Utils.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ import Foundation
1010

1111
class Utils {
1212

13+
static var sdkVersion:String?
14+
1315
// @objc NSNumber can be casted either Bool, Int, or Double
1416
// more filtering required to avoid NSNumber(false, true) interpreted as Int(0, 1) instead of Bool
1517

@@ -120,11 +122,16 @@ class Utils {
120122
// - Bundle(identifier: bundleIdentifier) works ok consistently
121123
// - CocoaPods uses its own bundle identifier, so let it use Bundle(for:) as a fallback
122124
// CocoaPods copies "s.version" in podspec to "CFBundleShortVersionString" in its own Info.plist file
125+
if let sdkVersion = sdkVersion {
126+
return sdkVersion
127+
}
123128

124129
let bundle = Bundle(identifier: "com.optimizely.OptimizelySwiftSDK") ?? Bundle(for: OptimizelyManager.self)
125130
guard let version = bundle.infoDictionary!["CFBundleShortVersionString"] as? String else {
126131
fatalError("Check if SDK framework identifier is correct")
127132
}
133+
134+
sdkVersion = version
128135

129136
return version
130137
}

0 commit comments

Comments
 (0)