From 3128cbb2699ff792b2e7d0125066e1c13e963c76 Mon Sep 17 00:00:00 2001 From: Andrew Son <24321494+Andoran90@users.noreply.github.com> Date: Fri, 10 Jan 2025 15:24:43 +0500 Subject: [PATCH] IOS-8546: Move CHHapticEngine init on background thread --- .../TangemSdk/UI/Main/HapticsEngine.swift | 44 ++++++++++--------- 1 file changed, 24 insertions(+), 20 deletions(-) diff --git a/TangemSdk/TangemSdk/UI/Main/HapticsEngine.swift b/TangemSdk/TangemSdk/UI/Main/HapticsEngine.swift index f91722c9..327e7cb9 100644 --- a/TangemSdk/TangemSdk/UI/Main/HapticsEngine.swift +++ b/TangemSdk/TangemSdk/UI/Main/HapticsEngine.swift @@ -117,28 +117,32 @@ class HapticsEngine { return } - do { - engine = try CHHapticEngine() - engine!.playsHapticsOnly = true - engine!.stoppedHandler = {[weak self] reason in - Log.debug("CHHapticEngine stop handler: The engine stopped for reason: \(reason.rawValue)") - self?.engineNeedsStart = true - } - engine!.resetHandler = {[weak self] in - Log.debug("Reset Handler: Restarting the engine.") - do { - // Try restarting the engine. - try self?.engine?.start() - - // Indicate that the next time the app requires a haptic, the app doesn't need to call engine.start(). - self?.engineNeedsStart = false - - } catch { - Log.error("Failed to start the engine with error: \(error)") + /// We need to instantiate `CHHapticEngine` on a background thread + /// because on the Main thread I/O operations can cause UI unresponsiveness + DispatchQueue.global(qos: .userInitiated).async { + do { + self.engine = try CHHapticEngine() + self.engine!.playsHapticsOnly = true + self.engine!.stoppedHandler = { [weak self] reason in + Log.debug("CHHapticEngine stop handler: The engine stopped for reason: \(reason.rawValue)") + self?.engineNeedsStart = true + } + self.engine!.resetHandler = { [weak self] in + Log.debug("Reset Handler: Restarting the engine.") + do { + // Try restarting the engine. + try self?.engine?.start() + + // Indicate that the next time the app requires a haptic, the app doesn't need to call engine.start(). + self?.engineNeedsStart = false + + } catch { + Log.error("Failed to start the engine with error: \(error)") + } } + } catch { + Log.error("CHHapticEngine error: \(error)") } - } catch { - Log.error("CHHapticEngine error: \(error)") } }