Skip to content

Commit afea95e

Browse files
configured start and stop to work correctly, also added timeout interval for sdk. intialize.
1 parent 984b0f2 commit afea95e

File tree

4 files changed

+34
-18
lines changed

4 files changed

+34
-18
lines changed

DemoSwiftApp/AppDelegate.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
9999
})
100100

101101
// initialize Optimizely Client from a datafile download
102-
optimizely!.initializeSDK { result in
102+
optimizely!.initializeSDK(resourceTimeout:0.001) { result in
103103
switch result {
104104
case .failure(let error):
105105
print("Optimizely SDK initiliazation failed: \(error)")

OptimizelySDK/Implementation/DefaultDatafileHandler.swift

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,13 @@ class DefaultDatafileHandler : OPTDatafileHandler {
4848
return datafile
4949
}
5050

51-
open func downloadDatafile(sdkKey: String, completionHandler: @escaping DatafileDownloadCompletionHandler) {
51+
open func downloadDatafile(sdkKey: String,
52+
resourceTimeoutInterval:Double = -1,
53+
completionHandler: @escaping DatafileDownloadCompletionHandler) {
5254
let config = URLSessionConfiguration.ephemeral
55+
if resourceTimeoutInterval > 0 {
56+
config.timeoutIntervalForResource = TimeInterval(resourceTimeoutInterval)
57+
}
5358
let session = URLSession(configuration: config)
5459
let str = String(format: DefaultDatafileHandler.endPointStringFormat, sdkKey)
5560
if let url = URL(string: str) {
@@ -100,19 +105,11 @@ class DefaultDatafileHandler : OPTDatafileHandler {
100105

101106
func startPeriodicUpdates(sdkKey: String, updateInterval: Int, datafileChangeNotification:((Data)->Void)?) {
102107

103-
timers.performAtomic { (timers) in
104-
if let timer = timers[sdkKey] {
105-
logger?.log(level: .info, message: "Timer getting restarted for datafile updates \(sdkKey)")
106-
timer.invalidate()
107-
timers[sdkKey] = nil
108-
//return
109-
}
110-
}
111108
if #available(iOS 10.0, tvOS 10.0, *) {
112109
DispatchQueue.main.async {
113110
let timer = Timer.scheduledTimer(withTimeInterval: TimeInterval(updateInterval), repeats: false) { (timer) in
114111

115-
self.downloadDatafile(sdkKey: sdkKey, completionHandler: { (result) in
112+
self.downloadDatafile(sdkKey: sdkKey) { (result) in
116113
switch result {
117114
case .success(let data):
118115
if let data = data,
@@ -122,8 +119,13 @@ class DefaultDatafileHandler : OPTDatafileHandler {
122119
case .failure(let error):
123120
self.logger?.log(level: .error, message: error.localizedDescription)
124121
}
125-
self.startPeriodicUpdates(sdkKey: sdkKey, updateInterval: updateInterval, datafileChangeNotification: datafileChangeNotification)
126-
})
122+
123+
if self.hasPeriodUpdates(sdkKey: sdkKey) {
124+
self.startPeriodicUpdates(sdkKey: sdkKey, updateInterval: updateInterval, datafileChangeNotification: datafileChangeNotification)
125+
}
126+
}
127+
128+
timer.invalidate()
127129
}
128130
self.timers.performAtomic(atomicOperation: { (timers) in
129131
timers[sdkKey] = timer
@@ -134,6 +136,17 @@ class DefaultDatafileHandler : OPTDatafileHandler {
134136
}
135137
}
136138

139+
func hasPeriodUpdates(sdkKey: String) -> Bool {
140+
var restart = true
141+
self.timers.performAtomic(atomicOperation: { (timers) in
142+
if !timers.contains(where: { $0.key == sdkKey} ) {
143+
restart = false
144+
}
145+
})
146+
147+
return restart
148+
}
149+
137150
func stopPeriodicUpdates(sdkKey: String) {
138151
timers.performAtomic { (timers) in
139152
if let timer = timers[sdkKey] {

OptimizelySDK/Optimizely/OptimizelyManager.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,8 @@ open class OptimizelyManager: NSObject {
7979
///
8080
/// - Parameters:
8181
/// - completion: callback when initialization is completed
82-
public func initializeSDK(completion: ((OptimizelyResult<Data>) -> Void)?=nil) {
83-
fetchDatafileBackground { result in
82+
public func initializeSDK(resourceTimeout:Double = -1,completion: ((OptimizelyResult<Data>) -> Void)?=nil) {
83+
fetchDatafileBackground(resourceTimeout:resourceTimeout) { result in
8484
switch result {
8585
case .failure:
8686
completion?(result)
@@ -176,12 +176,12 @@ open class OptimizelyManager: NSObject {
176176
}
177177
}
178178

179-
func fetchDatafileBackground(completion: ((OptimizelyResult<Data>) -> Void)?=nil) {
179+
func fetchDatafileBackground(resourceTimeout:Double = -1, completion: ((OptimizelyResult<Data>) -> Void)?=nil) {
180180

181181
// TODO: fix downloadDatafile to throw OptimizelyError
182182
// those errors propagated instead of handling here
183183

184-
datafileHandler.downloadDatafile(sdkKey: self.sdkKey){ result in
184+
datafileHandler.downloadDatafile(sdkKey: self.sdkKey, resourceTimeoutInterval:resourceTimeout){ result in
185185
var fetchResult: OptimizelyResult<Data>
186186

187187
switch result {

OptimizelySDK/Protocols/OPTDatafileHandler.swift

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,12 @@ public protocol OPTDatafileHandler {
4343
/**
4444
Asynchronous download data file.
4545
- Parameter sdkKey: application context for download
46+
- Parameter resourceTimeoutInterval: timeout in seconds to wait for resource.
4647
- Parameter completionHhandler: listener to call when datafile download complete
4748
*/
48-
func downloadDatafile(sdkKey:String, completionHandler:@escaping DatafileDownloadCompletionHandler)
49+
func downloadDatafile(sdkKey:String,
50+
resourceTimeoutInterval:Double,
51+
completionHandler:@escaping DatafileDownloadCompletionHandler)
4952

5053
/**
5154
Start periodic updates to the project datafile .

0 commit comments

Comments
 (0)