Skip to content

Commit 068f715

Browse files
allow timer dowloads in any os
1 parent 55a22c5 commit 068f715

File tree

1 file changed

+41
-15
lines changed

1 file changed

+41
-15
lines changed

OptimizelySDK/Implementation/DefaultDatafileHandler.swift

Lines changed: 41 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -109,21 +109,7 @@ class DefaultDatafileHandler : OPTDatafileHandler {
109109
DispatchQueue.main.async {
110110
let timer = Timer.scheduledTimer(withTimeInterval: TimeInterval(updateInterval), repeats: false) { (timer) in
111111

112-
self.downloadDatafile(sdkKey: sdkKey) { (result) in
113-
switch result {
114-
case .success(let data):
115-
if let data = data,
116-
let datafileChangeNotification = datafileChangeNotification {
117-
datafileChangeNotification(data)
118-
}
119-
case .failure(let error):
120-
self.logger?.log(level: .error, message: error.localizedDescription)
121-
}
122-
123-
if self.hasPeriodUpdates(sdkKey: sdkKey) {
124-
self.startPeriodicUpdates(sdkKey: sdkKey, updateInterval: updateInterval, datafileChangeNotification: datafileChangeNotification)
125-
}
126-
}
112+
self.performPerodicDownload(sdkKey: sdkKey, updateInterval: updateInterval, datafileChangeNotification: datafileChangeNotification)
127113

128114
timer.invalidate()
129115
}
@@ -133,9 +119,29 @@ class DefaultDatafileHandler : OPTDatafileHandler {
133119
}
134120
} else {
135121
// Fallback on earlier versions
122+
DispatchQueue.main.async {
123+
let timer = Timer.scheduledTimer(timeInterval: TimeInterval(updateInterval), target: self, selector:#selector(self.timerFired(timer:)), userInfo: ["sdkKey": sdkKey, "updateInterval":updateInterval, "datafileChangeNotification":datafileChangeNotification ?? { (data) in }], repeats: false)
124+
125+
self.timers.performAtomic(atomicOperation: { (timers) in
126+
timers[sdkKey] = timer
127+
})
128+
}
129+
136130
}
137131
}
138132

133+
@objc
134+
func timerFired(timer:Timer) {
135+
if let info = timer.userInfo as? [String:Any],
136+
let sdkKey = info["sdkKey"] as? String,
137+
let updateInterval = info["updateInterval"] as? Int,
138+
let datafileChangeNotification = info["datafileChangeNotification"] as? ((Data)->Void){
139+
self.performPerodicDownload(sdkKey: sdkKey, updateInterval: updateInterval, datafileChangeNotification: datafileChangeNotification)
140+
}
141+
timer.invalidate()
142+
143+
}
144+
139145
func hasPeriodUpdates(sdkKey: String) -> Bool {
140146
var restart = true
141147
self.timers.performAtomic(atomicOperation: { (timers) in
@@ -147,6 +153,26 @@ class DefaultDatafileHandler : OPTDatafileHandler {
147153
return restart
148154
}
149155

156+
func performPerodicDownload(sdkKey: String,
157+
updateInterval:Int,
158+
datafileChangeNotification:((Data)->Void)?) {
159+
self.downloadDatafile(sdkKey: sdkKey) { (result) in
160+
switch result {
161+
case .success(let data):
162+
if let data = data,
163+
let datafileChangeNotification = datafileChangeNotification {
164+
datafileChangeNotification(data)
165+
}
166+
case .failure(let error):
167+
self.logger?.log(level: .error, message: error.localizedDescription)
168+
}
169+
170+
if self.hasPeriodUpdates(sdkKey: sdkKey) {
171+
self.startPeriodicUpdates(sdkKey: sdkKey, updateInterval: updateInterval, datafileChangeNotification: datafileChangeNotification)
172+
}
173+
}
174+
}
175+
150176
func stopPeriodicUpdates(sdkKey: String) {
151177
timers.performAtomic { (timers) in
152178
if let timer = timers[sdkKey] {

0 commit comments

Comments
 (0)