diff --git a/ConfigCat.podspec b/ConfigCat.podspec index 2b19650..3601738 100755 --- a/ConfigCat.podspec +++ b/ConfigCat.podspec @@ -1,7 +1,7 @@ Pod::Spec.new do |spec| spec.name = "ConfigCat" - spec.version = "9.2.3" + spec.version = "9.2.4" spec.summary = "ConfigCat Swift SDK" spec.swift_version = "4.2" diff --git a/ConfigCat.xcconfig b/ConfigCat.xcconfig index 33ef61e..0439b24 100644 --- a/ConfigCat.xcconfig +++ b/ConfigCat.xcconfig @@ -47,4 +47,4 @@ SUPPORTED_PLATFORMS = macosx iphoneos iphonesimulator watchos watchsimulator app SWIFT_VERSION = 4.2 // ConfigCat SDK version -MARKETING_VERSION = 9.2.3 +MARKETING_VERSION = 9.2.4 diff --git a/README.md b/README.md index b419f23..b50c3c5 100644 --- a/README.md +++ b/README.md @@ -49,7 +49,7 @@ If you want to use ConfigCat in a [SwiftPM](https://swift.org/package-manager/) ``` swift dependencies: [ - .package(url: "https://github.com/configcat/swift-sdk", from: "9.2.3") + .package(url: "https://github.com/configcat/swift-sdk", from: "9.2.4") ] ``` diff --git a/Sources/ConfigCat/ConfigService.swift b/Sources/ConfigCat/ConfigService.swift index cbf5d95..dc4988a 100644 --- a/Sources/ConfigCat/ConfigService.swift +++ b/Sources/ConfigCat/ConfigService.swift @@ -33,7 +33,7 @@ class ConfigService { private let pollingMode: PollingMode private let hooks: Hooks private let cacheKey: String - private var initialized: Bool + private var initialized: Bool = false private var offline: Bool = false private var completions: MutableQueue<(FetchResult) -> Void>? private var cachedEntry: ConfigEntry = .empty @@ -51,12 +51,9 @@ class ConfigService { let keyToHash = "swift_" + Constants.configJsonName + "_" + sdkKey cacheKey = String(keyToHash.sha1hex ?? keyToHash) - if let autoPoll = pollingMode as? AutoPollingMode { - initialized = false + if let autoPoll = pollingMode as? AutoPollingMode, !offline { - if !offline { - startPoll(mode: autoPoll) - } + startPoll(mode: autoPoll) // Waiting for the client initialization. // After the maxInitWaitTimeInSeconds timeout the client will be initialized and while the config is not ready @@ -80,8 +77,7 @@ class ConfigService { }) initTimer?.resume() } else { - initialized = true - hooks.invokeOnReady() + setInitialized() } } @@ -170,10 +166,7 @@ class ConfigService { } // Cache isn't expired if cachedEntry.fetchTime > time { - if !initialized { - initialized = true - hooks.invokeOnReady() - } + setInitialized() completion(.success(cachedEntry)) return } @@ -207,10 +200,7 @@ class ConfigService { mutex.lock() defer { mutex.unlock() } - if !initialized { - initialized = true - hooks.invokeOnReady() - } + setInitialized() switch response { case .fetched(let entry): cachedEntry = entry @@ -230,6 +220,13 @@ class ConfigService { completions = nil } + private func setInitialized() { + if !initialized { + initialized = true + hooks.invokeOnReady() + } + } + private func callCompletions(result: FetchResult) { if let completions = completions { while !completions.isEmpty { diff --git a/Sources/ConfigCat/Utils.swift b/Sources/ConfigCat/Utils.swift index 9a6e8ae..3fdc7ef 100644 --- a/Sources/ConfigCat/Utils.swift +++ b/Sources/ConfigCat/Utils.swift @@ -38,7 +38,7 @@ extension Date { } class Constants { - static let version: String = "9.2.3" + static let version: String = "9.2.4" static let configJsonName: String = "config_v5" static let globalBaseUrl: String = "https://cdn-global.configcat.com" static let euOnlyBaseUrl: String = "https://cdn-eu.configcat.com" diff --git a/Tests/ConfigCatTests/ConfigCatClientTests.swift b/Tests/ConfigCatTests/ConfigCatClientTests.swift index 92b9242..8fbb514 100755 --- a/Tests/ConfigCatTests/ConfigCatClientTests.swift +++ b/Tests/ConfigCatTests/ConfigCatClientTests.swift @@ -436,6 +436,25 @@ class ConfigCatClientTests: XCTestCase { XCTAssertFalse(client.isOffline) } + func testInitOfflineCallsReady() { + let engine = MockEngine() + engine.enqueueResponse(response: Response(body: String(format: testJsonFormat, "\"fake\""), statusCode: 200)) + var ready = false + let hooks = Hooks() + hooks.addOnReady { + ready = true + } + let client = ConfigCatClient(sdkKey: "test", pollingMode: PollingModes.autoPoll(), httpEngine: engine, hooks: hooks, offline: true) + let expectation = self.expectation(description: "wait for response") + client.getValue(for: "fakeKey", defaultValue: "") { _ in + expectation.fulfill() + } + wait(for: [expectation], timeout: 5) + + XCTAssertEqual(0, engine.requests.count) + XCTAssertTrue(ready) + } + func testDefaultUser() { let engine = MockEngine() engine.enqueueResponse(response: Response(body: createTestConfigWithRules().toJsonString(), statusCode: 200))