diff --git a/LaunchDarkly/LaunchDarkly/LDClient.swift b/LaunchDarkly/LaunchDarkly/LDClient.swift index 411de932..09b18421 100644 --- a/LaunchDarkly/LaunchDarkly/LDClient.swift +++ b/LaunchDarkly/LaunchDarkly/LDClient.swift @@ -115,8 +115,10 @@ public class LDClient { instance.internalSetOnline(goOnline, completion: dispatch.leave) } } - if let completion = completion { - dispatch.notify(queue: DispatchQueue.global(), execute: completion) + if let completion { + dispatch.notify(queue: .main) { + completion() + } } } @@ -292,7 +294,9 @@ public class LDClient { public func identify(context: LDContext, completion: (() -> Void)? = nil) { _identifyHooked(context: context, sheddable: false, useCache: .yes, timeout: 0) { _ in if let completion = completion { - completion() + DispatchQueue.main.async { + completion() + } } } } @@ -313,7 +317,11 @@ public class LDClient { - parameter completion: Closure called when the embedded `setOnlineIdentify` call completes, subject to throttling delays. */ public func identify(context: LDContext, completion: @escaping (_ result: IdentifyResult) -> Void) { - _identifyHooked(context: context, sheddable: true, useCache: .yes, timeout: 0, completion: completion) + _identifyHooked(context: context, sheddable: true, useCache: .yes, timeout: 0) { result in + DispatchQueue.main.async { + completion(result) + } + } } /** @@ -327,7 +335,11 @@ public class LDClient { - parameter completion: Closure called when the embedded `setOnlineIdentify` call completes, subject to throttling delays. */ public func identify(context: LDContext, useCache: IdentifyCacheUsage, completion: @escaping (_ result: IdentifyResult) -> Void) { - _identifyHooked(context: context, sheddable: true, useCache: useCache, timeout: 0, completion: completion) + _identifyHooked(context: context, sheddable: true, useCache: useCache, timeout: 0) { result in + DispatchQueue.main.async { + completion(result) + } + } } // Temporary helper method to allow code sharing between the sheddable and unsheddable identify methods. In the next major release, we will remove the deprecated identify method and inline @@ -385,7 +397,11 @@ public class LDClient { os_log("%s LDClient.identify was called with a timeout greater than %f seconds. We recommend a timeout of less than %f seconds.", log: config.logger, type: .info, self.typeName(and: #function), LDClient.longTimeoutInterval, LDClient.longTimeoutInterval) } - self._identifyHooked(context: context, sheddable: true, useCache: useCache, timeout: timeout, completion: completion) + self._identifyHooked(context: context, sheddable: true, useCache: useCache, timeout: timeout) { result in + DispatchQueue.main.async { + completion(result) + } + } } func internalIdentify(newContext: LDContext, useCache: IdentifyCacheUsage, completion: (() -> Void)? = nil) { @@ -741,7 +757,13 @@ public class LDClient { /// - Tag: start @available(*, deprecated, message: "Use LDClient.start(config: context: startWithSeconds: completion:) to initialize the SDK with a defined timeout") public static func start(config: LDConfig, context: LDContext? = nil, completion: (() -> Void)? = nil) { - start(serviceFactory: nil, config: config, context: context, completion: completion) + start(serviceFactory: nil, config: config, context: context) { + if let completion { + DispatchQueue.main.async { + completion() + } + } + } } static func start(serviceFactory: ClientServiceCreating?, config: LDConfig, context: LDContext? = nil, completion: (() -> Void)? = nil) { @@ -836,7 +858,13 @@ public class LDClient { os_log("%s LDClient.start was called with a timeout greater than %f seconds. We recommend a timeout of less than %f seconds.", log: config.logger, type: .info, self.typeName(and: #function), LDClient.longTimeoutInterval, LDClient.longTimeoutInterval) } - start(serviceFactory: nil, config: config, context: context, startWaitSeconds: startWaitSeconds, completion: completion) + start(serviceFactory: nil, config: config, context: context, startWaitSeconds: startWaitSeconds) { timedOut in + if let completion { + DispatchQueue.main.async { + completion(timedOut) + } + } + } } static func start(serviceFactory: ClientServiceCreating?, config: LDConfig, context: LDContext? = nil, startWaitSeconds: TimeInterval, completion: ((_ timedOut: Bool) -> Void)? = nil) {