From d87adc6587ede073365cc992a70066600c3ef0d5 Mon Sep 17 00:00:00 2001 From: kenji Date: Tue, 25 Jul 2023 15:25:59 +0700 Subject: [PATCH] fix: remove precondition() from BTKClient --- Bucketeer/Sources/Public/BKTClient.swift | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/Bucketeer/Sources/Public/BKTClient.swift b/Bucketeer/Sources/Public/BKTClient.swift index a0c1c905..46cbada2 100644 --- a/Bucketeer/Sources/Public/BKTClient.swift +++ b/Bucketeer/Sources/Public/BKTClient.swift @@ -73,8 +73,10 @@ public class BKTClient { } extension BKTClient { - public static func initialize(config: BKTConfig, user: BKTUser, timeoutMillis: Int64 = 5000, completion: ((BKTError?) -> Void)? = nil) { - precondition(Thread.isMainThread, "the initialize method must be called on main thread") + public static func initialize(config: BKTConfig, user: BKTUser, timeoutMillis: Int64 = 5000, completion: ((BKTError?) -> Void)? = nil) throws { + guard (Thread.isMainThread) else { + throw BKTError.illegalState(message: "the initialize method must be called on main thread") + } concurrentQueue.sync { guard BKTClient.default == nil else { config.logger?.warn(message: "BKTClient is already initialized. Not sure if the initial fetch has finished") @@ -98,22 +100,23 @@ extension BKTClient { } } - public static func destroy() { - precondition(Thread.isMainThread, "the destroy method must be called on main thread") + public static func destroy() throws { + guard (Thread.isMainThread) else { + throw BKTError.illegalState(message: "the destroy method must be called on main thread") + } BKTClient.default?.resetTasks() BKTClient.default = nil } public static var shared: BKTClient { get throws { + // We do not want to crash the SDK's consumer app on runtime by using fatalError(). + // So let the app has a chance to catch this exception + // The same behavior with the Android SDK guard BKTClient.default != nil else { throw BKTError.illegalState(message: "BKTClient is not initialized") } return BKTClient.default - // We do not want to crash the SDK's consumer app on runtime by using fatalError(). - // So let the app has a chance to catch this exception - // The same behavior with the Android SDK - } }