Skip to content

Commit a5eb95b

Browse files
(chore): refactor atomic property to allow for atomic operations (#121)
* refactor atomic property to allow for atomic operations * add one more performAtomic
1 parent f11e5bb commit a5eb95b

File tree

3 files changed

+18
-6
lines changed

3 files changed

+18
-6
lines changed

OptimizelySDK/Customization/DefaultEventDispatcher.swift

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ open class DefaultEventDispatcher : BackgroundingCallbacks, OPTEventDispatcher {
6767
}
6868

6969
deinit {
70-
if let timer = timer.property {
70+
timer.performAtomic() { (timer) in
7171
timer.invalidate()
7272
}
7373
unsubscribe()
@@ -206,7 +206,7 @@ open class DefaultEventDispatcher : BackgroundingCallbacks, OPTEventDispatcher {
206206
}
207207

208208
func applicationDidEnterBackground() {
209-
if let timer = timer.property {
209+
timer.performAtomic() { (timer) in
210210
timer.invalidate()
211211
}
212212
timer.property = nil
@@ -231,7 +231,9 @@ open class DefaultEventDispatcher : BackgroundingCallbacks, OPTEventDispatcher {
231231
DispatchQueue.main.async {
232232
self.timer.property = Timer.scheduledTimer(withTimeInterval: self.timerInterval, repeats: true) { (timer) in
233233
if self.dataStore.count == 0 {
234-
self.timer.property?.invalidate()
234+
self.timer.performAtomic() { (timer) in
235+
timer.invalidate()
236+
}
235237
self.timer.property = nil
236238
}
237239
else {

OptimizelySDK/OptimizelyTests/OptimizelyTests-APIs/OptimizelyManagerTests_Threading.swift

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,6 @@ class OptimizelyManagerTests_Threading: XCTestCase {
260260
//let expectationBackground = XCTestExpectation(description: "waiting for background thread")
261261
let backgroundQueue = DispatchQueue(label: "mybackground", qos: DispatchQoS.default, attributes: DispatchQueue.Attributes.concurrent)
262262
let atomicBackground = AtomicProperty<[(enabled:Bool?, variation:String?)]>(property: [(enabled:Bool?, variation:String?)]())
263-
let appendLock = DispatchQueue(label: "appendLock")
264263

265264
let dispatchGroup = DispatchGroup()
266265

@@ -270,8 +269,8 @@ class OptimizelyManagerTests_Threading: XCTestCase {
270269
let enabled = try? optimizely2.isFeatureEnabled(featureKey: "feat", userId: self.userId, attributes: ["house": "Gryffindor"])
271270
let variation = try? optimizely3.activate(experimentKey: "ab_running_exp_untargeted", userId: self.userId)
272271

273-
appendLock.sync {
274-
atomicBackground.property!.append((enabled: enabled!, variation: variation!))
272+
atomicBackground.performAtomic() { (array) in
273+
array.append((enabled: enabled!, variation: variation!))
275274
}
276275

277276
defer {

OptimizelySDK/Utils/AtomicProperty.swift

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,5 +38,16 @@ class AtomicProperty<T> {
3838
init() {
3939

4040
}
41+
42+
// perform an atomic operation on the atomic property
43+
// the operation will not run if the property is nil.
44+
public func performAtomic(atomicOperation:((_ prop:inout T)->Void)) {
45+
lock.sync {
46+
if var prop = _property {
47+
atomicOperation(&prop)
48+
_property = prop
49+
}
50+
}
51+
}
4152

4253
}

0 commit comments

Comments
 (0)