Skip to content

Commit

Permalink
IOS-8546: Move CHHapticEngine init on background thread
Browse files Browse the repository at this point in the history
  • Loading branch information
Andoran90 committed Jan 13, 2025
1 parent e7faf52 commit 3128cbb
Showing 1 changed file with 24 additions and 20 deletions.
44 changes: 24 additions & 20 deletions TangemSdk/TangemSdk/UI/Main/HapticsEngine.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)")
}
}

Expand Down

0 comments on commit 3128cbb

Please sign in to comment.